Reactjs: Key undefined when accessed as a prop

前端 未结 3 835
抹茶落季
抹茶落季 2020-12-31 06:19

Tools: Reactjs 0.14.0 Vanilla Flux

I need unique identifiers for 2 reasons:

  1. Child Reconciliation
  2. Keeping track of what child
相关标签:
3条回答
  • 2020-12-31 06:34

    It is best to use id. Then in the eventHandler you can have event.target.id.

    function getMessageListItem(message) {
    
      return (
        <MessageListItem key={message.id} id={message.id} message={message}/>
      );
    }

    0 讨论(0)
  • 2020-12-31 06:52

    key and ref aren't really 'props'. They're used internally by react and not passed to components as props. Consider passing it as a prop such as 'id'.

    0 讨论(0)
  • 2020-12-31 07:01

    As we already established in other answers that you can't use key since it isn't passed as a prop and instead used internally by react. Here is my 2 cents as an alternative:
    Since you're passing the entire array of messages as a prop while creating the <MessageListItem> component, you don't necessarily need to pass another prop with id. You can simply use {this.props.message.id} whenever you need to use id.

    0 讨论(0)
提交回复
热议问题