我在try 从API URL获取数据时遇到了一个问题,问题是数据不是通过SectionList中的类别标题返回的,而是在标题[object Object]而不是renderSectionHeader下返回的.

我的问题复制零食expo : https://snack.expo.dev/@fyardlest/resto-app?platform=android

enter image description here

推荐答案

您的数据类型架构与SectionList要求不匹配.请看一下给定的链接.您的数据模式如下所示:

[
  { category: {title: 'Appetizers'}, id: 1, price: "10", title: "Spinach Artichoke Dip" },
  ~~~
  ~~~
]

SectionList成分股预计会出现以下情况:

[
  {
    title: 'Main dishes',
    data: ['Pizza', 'Burger', 'Risotto'],
  },
]

但是,除了第一个数组级别之外,SectionList还需要更深一级.所以我觉得你应该用FlatList美元.


然而,主要的问题来自你的saveMenuItems函数:

export function saveMenuItems(menuItems) {
  db.transaction((tx) => {
    const values = menuItems
      .map(
        (item) =>
          `('${item.id}', '${item.title}', '${item.price}', '${item.category}')`
      )
      .join(', ');

    tx.executeSql(
      `insert into menuitems (uuid, title, price, category) values ${values};`
    );
  });
}

此函数将category设置为[object Object],因为您使用的是${item.category},而item.categorty是这样的对象:{ title: 'something' }.然后在getSectionListData内部,问题本身出现,并产生另一个问题.

我认为你应该在saveMenuItems函数中使用${item.category.title}.

React-native相关问答推荐

添加REACT-Native-SVG时出现错误-失败:构建失败并出现异常

在react-native 应用程序中使用中继

在根项目ReactNativeStarter中找不到任务installRelease

为 React Native TextInput 创建 ref 时出现 TypeScript 问题

xcrun:错误:SDK "iphoneos" cannot be located

React-Native + crypto:如何在 React-Native 中生成 HMAC?

React Native localhost 另一个调试器已经连接

React native expection java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/{package}/lib-main/libgnustl_shared.so" is 32-bit instead of 64-bit

react-native async 函数返回 promise 但不返回我的 json 数据?

react-native图标

axios 的网络错误和react-native

React Native Android 文本组件额外填充

React Native vs Swift/Objective-C/Java Native

react-native Bottom Up drawer

Ipad 上的 React Native - 错误 - could not connect to development server

如何在react-native中绘制虚线边框样式

如何从 React-Native 中的另一个类调用函数?

在 React Native 中将props传递给外部样式表?

类型 androidx.appcompat.resources.R$dimen 被定义了多次

保存时 Visual Studio 代码格式化失败