简体   繁体   English

React不会将特定的值传递给“ props”

[英]React won't pass a particular value to “props”

I'm having a strange issue that I can't pin down with React (I'm using CoffeeScript as well, but I highly doubt this is a factor). 我有一个奇怪的问题,我无法用React来解决(我也在使用CoffeeScript,但我高度怀疑这是一个因素)。 Basically, I'm following along with a tutorial in which a message feed is built using a Feed component (the parent), FeedList component (child), and a FeedItem (grandchild)...sorry if my terminology is incorrect. 基本上,我将跟随一个教程,其中使用Feed组件(父组件),FeedList组件(子组件)和FeedItem(孙子组件)来构建消息源...抱歉,如果我的术语不正确。 The relevant code is: 相关代码为:

Feed.cjsx Feed.cjsx

getInitialState: ->
  FEED_ITEMS = [
    { key: 1, title: 'Realtime data!', description: 'Firebase is cool', voteCount: 49 }
    { key: 2, title: 'JavaScript is fun', description: 'Lexical scoping FTW', voteCount: 34 }
    { key: 3, title: 'Coffee makes you awake', description: 'Drink responsibly', voteCount: 15 }
  ]
  {
    items: FEED_ITEMS
    formDisplayed: false
  }
  ...

render: ->
  ...
  <FeedList items={@state.items} onVote={@onVote} />

FeedList.cjsx FeedList.cjsx

render: ->

  feedItems = @props.items.map ((item) -> 
    <FeedItem key={item.key} ... />
  ).bind(@)

  <ul className='list-group container'>
    {feedItems}
  </ul>

FeedItem.cjsx FeedItem.cjsx

render: ->
  <li key={@props.key} className='list-group-item'>
    ...
  </li>

If I console.log "@props.key" in the render method for FeedItem, I get undefined. 如果在FeedItem的渲染方法中使用console.log“ @ props.key”,则无法定义。 But if I log "item.key" from inside the map function of FeedList's render method, I get 1, 2, 3, as I should. 但是,如果我从FeedList的render方法的map函数内部记录“ item.key”,则应该得到1、2、3。 So it seems to me that, for whatever reason, React doesn't want to pass the "key" prop to the FeedItem. 因此在我看来,无论出于何种原因,React都不希望将“关键”道具传递给FeedItem。 Any thoughts? 有什么想法吗?

For anyone else stumbling across this, react only has a few reserved props but they are worth noting. 对于任何绊脚石的人来说,react只有几个保留的道具,但它们值得注意。 key, ref, __self and __source. 键,ref,__ self和__source。

var RESERVED_PROPS = { key: true, ref: true, __self: true, __source: true };

^^ Taken from the react source. ^^来自反应源。

Also worth noting __self={this} is really useful if you're receiving invariant violation errors and would like to be able to debug them down to a component level. 另外值得注意的是__self={this}在接收到__self={this}违规错误并希望能够将其调试到组件级别时非常有用。

Since react treats key as a special attribute ( http://facebook.github.io/react/docs/special-non-dom-attributes.html ), it cannot be accessed via the props. 由于react将密钥视为特殊属性( http://facebook.github.io/react/docs/special-non-dom-attributes.html ),因此无法通过道具进行访问。 The react documentation also warns against setting keys within plain html tags ( http://facebook.github.io/react/docs/multiple-components.html#dynamic-children ), and suggests wrapping multiple components in a react component. react文档还警告您不要在纯html标签( http://facebook.github.io/react/docs/multiple-components.html#dynamic-children )内设置键,并建议将多个组件包装在react组件中。

If you rename key to something non-reserved, it should work: 如果您将key重命名为非保留的名称,它将可以正常工作:

Feed.cjsx: Feed.cjsx:

FEED_ITEMS = [
    { itemId: 1, title: 'Realtime data!', description: 'Firebase is cool', voteCount: 49 }
    { itemId: 2, title: 'JavaScript is fun', description: 'Lexical scoping FTW', voteCount: 34 }
    { itemId: 3, title: 'Coffee makes you awake', description: 'Drink responsibly', voteCount: 15 }
]

then you can access the itemId via @props.itemId in the child component (FeedList). 那么您可以通过子组件(FeedList)中的@props.itemId访问itemId。

FeedList: FeedList:

feedItems = @props.items.map ((item) -> 
    <FeedItem key={item.itemId} ... />
).bind(@)

Note that the keys for each component need to be unique for each component, or node in the DOM, which is why it makes sense that keys cannot be inherited, as setting both parent and child to the same key would not allow react to identify them as separate entities when rendering the DOM. 请注意,每个组件的键对于DOM中的每个组件或节点都必须是唯一的,这就是为什么不能继承键是有道理的,因为将父键和子键都设置为同一键将不允许做出反应来识别它们在呈现DOM时作为单独的实体。

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

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