【问题标题】:can i pass two data to FlatList in react-native?我可以在 react-native 中将两个数据传递给 FlatList 吗?
【发布时间】:2021-03-12 14:23:45
【问题描述】:

我想将两个数据(即通知、回复通知这些数据是数组)传递给 FlatList 中 通知卡组件

有可能吗??这是我的代码

      const Notification = () => {
      const {notification, replynotification} = useSelector((state) => state.post);
      const dispatch  = useDispatch();

      useEffect(() => {
        dispatch({
          type:CHECKNOTIFICATION_REQUEST,
        })
      },[]);
        return (
        <FlatList
        data={notification}
        keyExtractor={(item) => String(item.id)}
        renderItem={({item}) => (
          <NotifiCard
          item={item}
          />
        )}
        />
        );
    };

    export default Notification;

那我该如何修复我的代码??

【问题讨论】:

  • 回复通知是否与通知相关?为什么没有两个不同的列表
  • @UdenduAbasili 是的,它相关但它必须区分这两个数据我该怎么做?

标签: javascript node.js reactjs react-native


【解决方案1】:

我希望我已经正确理解了这个问题。 如果您有两个不同的数组,notification 和 replynotification,并且您想将它们作为单个数组提供给 FlatList,您应该使用传播运算符(...),这样您就可以将所有数据传播到一个唯一的数组中。

const array = [
  ...notification,
  ...replynotification
]

通过这种方式,您将拥有一个包含您需要的所有数据的数组。 请注意,如果 notification 和 replynotification 具有不同的结构,您将拥有一个包含具有不同结构的项目的数组。

<FlatList
 data={array}
 keyExtractor={(item) => String(item.id)}
 renderItem={({item}) => (
  <NotifiCard
   item={item}
  />
 )}
/>

【讨论】:

  • 谢谢你,如果我使用你的代码,我可以在 Notificard 中获取数据.. 但是通知数据和回复通知数据没有区别,并且是合并的。我怎样才能得到杰出的数据?
  • 你能告诉我数据结构吗?无论如何,你可以使用对象数组,所以你可以给你的项目一个“种类”或“类型”属性来区分它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
相关资源
最近更新 更多