【问题标题】:RenderFeed function of react-native FlatList - Error: Objects are not valid as a React childreact-native FlatList 的 RenderFeed 函数 - 错误:对象作为 React 子项无效
【发布时间】:2020-07-23 09:37:36
【问题描述】:

完整的错误文本

错误:对象作为 React 子对象无效(找到:带有键 {_40、_65、_55、_72} 的对象)。如果您打算渲染一组子项,请改用数组。

renderFeed = async (item) => {
  let token = await localStorage.getItem('userToken')

  let data = await axios
    .get(ReactionsURL, {
      headers: {
        "token": token,
      },
    })
    .then((responseData) => {
      console.log("responseData", responseData);
      // Scope to use reactions
    })
    .catch((error) => {});
    

  const imgsrc =item.item.photo == null ? "../photos/defaultAvatar.png" : item.item.photo;


  return (
    <TouchableHighlight
      underlayColor="#F6F8FC"
      onPress={() =>
        navigation.navigate({
          name: "Details",
          params: {
            nav: navigation,
            id: item.item._id,
          },
        })
      }
    >
      <View style={styles.styleCard}>
        <View style={styles.TextWrapper}>
          <Title
            numberOfLines={4}
            ellipsizeMode="tail"
            style={styles.styleTitle}
          >
            {item.item.title}
          </Title>
          <Paragraph style={styles.Textdate}>
            {parseInt(new Date().getFullYear()) ===
            parseInt(moment(item.item.lastEdit).format("Y"))
              ? moment(item.item.lastEdit).format("dddd Do MMMM")
              : moment(item.item.lastEdit).format("dddd Do MMMM Y")}
          </Paragraph>
        </View>
        <Surface style={styles.Viewphoto}>
          <Image source={{ uri: imgsrc }} style={styles.photo} />
        </Surface>
      </View>
    </TouchableHighlight>

  );
};

我没有任何对象作为 React 孩子,我该如何解决这个错误?

【问题讨论】:

  • this : 带有键 {_40,_65,_55,_72} 的对象是一个未解决的承诺。删除 async/await 。处理异步情况我推荐这篇文章:medium.com/capbase-engineering/…

标签: javascript react-native


【解决方案1】:

试试

localStorage.getItem('userToken')

在顶部导入您的图像,然后更新您的 imgSrc。另外,尝试使用带有 img src 的可选链接运算符:

import defaultImage from "../photos/defaultAvatar.png"; //goes to top of file

const imgsrc = item?.item?.photo || defaultImage ;

【讨论】:

  • 您没有导入图像。可能是因为这个
【解决方案2】:

我通过从呈现的函数中删除异步等待解决了这个问题。

我在另一个函数中实现了获取请求

【讨论】:

    猜你喜欢
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多