简体   繁体   English

Reactjs:作为道具访问时未定义的键

[英]Reactjs: Key undefined when accessed as a prop

Tools: Reactjs 0.14.0 Vanilla Flux工具:Reactjs 0.14.0 Vanilla Flux

I need unique identifiers for 2 reasons:我需要唯一标识符有两个原因:

  1. Child Reconciliation 儿童和解
  2. Keeping track of what child was clicked 跟踪点击了什么孩子

So let's say I have a list of messages that looks like this:因此,假设我有一个如下所示的消息列表:

[
    {
      id: 1241241234,  // <-----The unique id is kept here
      authorName: "Nick"
      text: "Hi!"
    },
    ...
]

And now I use a Array.prototype.map() to create "ownee" component ( MessageListItem ) inside of the owner component MessageSection现在我使用Array.prototype.map()在所有者组件MessageSection内创建“拥有者”组件( MessageListItem

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

var MessageSection = React.createClass({
    render: function() {
        var messageListItems = this.state.messages.map(getMessageListItem);
        <div>
            {messageListItems }
        </div>
    }
});

But the this.props.key is undefined in the MessageListItem even though I know for a fact that is was defined when it was passed down.但是this.props.keyMessageListItem未定义,即使我知道它在传递时已定义的事实。

var ConvoListItem = React.createClass({
    render: function() {
        console.log(this.props.key); // Undefined
    }
});

I'm guessing there is a reason that React is not letting key be used as a prop.我猜 React 不让key用作道具是有原因的。

Question:题:

If I can't use key as a prop, then what is the proper way to handle the duality need of keying and setting unique identifiers on a dynamic list of child elements that contain state?如果我不能使用key作为道具,那么在包含状态的子元素的动态列表上处理键控和设置唯一标识符的二元性需求的正确方法是什么?

key and ref aren't really 'props'. key 和 ref 并不是真正的“道具”。 They're used internally by react and not passed to components as props.它们由 react 在内部使用,而不是作为 props 传递给组件。 Consider passing it as a prop such as 'id'.考虑将其作为道具传递,例如“id”。

It is best to use id.最好使用id。 Then in the eventHandler you can have event.target.id.然后在 eventHandler 中,您可以拥有 event.target.id。

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

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.正如我们在其他答案中已经确定的那样,您不能使用key因为它不是作为道具传递的,而是由 react 在内部使用的。 Here is my 2 cents as an alternative:这是我的 2 美分作为替代:
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 .由于您在创建<MessageListItem>组件时将整个消息数组作为道具传递,因此您不一定需要传递另一个带有id道具。 You can simply use {this.props.message.id} whenever you need to use id.只要您需要使用 id,您就可以简单地使用{this.props.message.id}

As you've discovered, the key property is consumed by React itself, not passed to the child component.正如您所发现的, key属性由 React 本身使用,而不是传递给子组件。 It's what React uses to keep track of entries in arrays to avoid unnecessarily re-rendering them (both in terms of calling their render and reconciling with the DOM). React 使用它来跟踪数组中的条目以避免不必要地重新渲染它们(在调用它们的render和与 DOM 协调方面)。

Unfortunately, that means if you also want to use it in the child, you have to pass it twice.不幸的是,这意味着如果您还想在 child 中使用它,则必须传递两次。 In your code, you already are passing it twice (once as key , one as part of message ).在您的代码中,您已经将它传递了两次(一次作为key ,一次作为message一部分)。 So you can keep your current JSX, just use id from message in the child:因此,您可以保留当前的 ​​JSX,只需在 child 中使用message中的id

var MessageListItem = React.createClass({
    render: function() {
        console.log(this.props.message.id);
//                             ^^^^^^^^^^^−−−−− Has the value
    }
});

More:更多的:

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

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