简体   繁体   English

无法传递 state 道具,表示未定义

[英]Can't pass the state props, says undefined

I have bee trying to pass the notes data to another component But I don't know why it says undefined.我有蜜蜂试图将笔记数据传递给另一个组件但我不知道为什么它说未定义。

export default App = () => {
  const [notes, setNotes] = useState([
    { key: 1, value: "Hello there what up", title: "Some Title for this" },
    { key: 2, value: "I want to take notes", title: "another for this one" },
  ]);

  return (
    <View>
      <FlatList
        data={notes}
        renderItem={({ note }) => (
          <View>
            <NoteItem note={note} />
          </View>
        )}
      />
    </View>
  );
};
export default NoteItem = ({ note }) => {
    <Text>{note.title}</Text>
  );
};

First of you assume that renderItem in Flatlist has a 'note' property inside it.首先假设 Flatlist 中的 renderItem 里面有一个 'note' 属性。 It does not, it is called item.它没有,它被称为项目。 Also you pass notes but you try to deconstruct note?你也传递笔记,但你试图解构笔记? It should be like this:它应该是这样的:

export default App = () => {
  const [notes, setNotes] = useState([
    { key: 1, value: "Hello there what up", title: "Some Title for this" },
    { key: 2, value: "I want to take notes", title: "another for this one" },
  ]);

  return (
    <View>
      <FlatList
        data={notes}
        renderItem={({ item }) => (
          <View>
            <NoteItem note={item} />
          </View>
        )}
      />
    </View>
  );
};

This syntax - ({ note }) - means that the NoteItem component expects a prop named note to be set;这种语法 - ({ note }) - 意味着NoteItem组件需要设置一个名为note的 prop; however, you're setting the notes prop instead.但是,您正在设置notes道具。

Try this:尝试这个:

<FlatList
   data={notes}
   renderItem={({ item, index }) => (
     <View key={index}>
       <NoteItem note={item} />
     </View>
   )}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM