简体   繁体   English

警告:每个孩子都应该有一个唯一的键 - 在 ReactJS 中传递数组

[英]Warning: Each child should have a unique key - Passing Array In ReactJS

I was practicing ReactJS tuts on Scrimba where you have to pass id props in array我在 Scrimba 上练习 ReactJS tuts,你必须在数组中传递 id 道具

 import React from 'react'; import Joke from './components/Joke.js' import jokesData from './components/jokesData'; function App() { const jokeComponents = jokesData.map(function(joke) { return ( <Joke obj={{key: joke.id, question: joke.question, punchline: joke.punchLine}} /> ) }) return ( <div> {jokeComponents} </div> ) } export default App;

Then I got a warning in console:然后我在控制台中收到警告:

Warning log警告日志

警告日志

In the image above the key value was passed but I still got the warning message.在上图中,键值已通过,但我仍然收到警告消息。 I'm trying to pass the elements into a single object.我正在尝试将元素传递到单个对象中。 Can anyone help me find the problem here?谁能帮我在这里找到问题?

You can add key attribute and pass in that joke.id .您可以添加key属性并传入该joke.id

  const jokeComponents = jokesData.map(function(joke) {
    return (
      <Joke 
        obj={{key: joke.id, question: joke.question, punchline: joke.punchLine}}
        key={joke.id)
      />
    )
  })

You have to pass key as prop to Joke component.您必须将密钥作为道具传递给 Joke 组件。 id would be great choice because of how react reconciliation work. id 将是不错的选择,因为 react 和解的工作方式。

<Joke  key={joke.id}
            obj={{key: joke.id, question: joke.question, punchline: joke.punchLine}} 
          />

暂无
暂无

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

相关问题 Reactjs:警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Reactjs: Warning Each child in a list should have a unique "key" prop 数组或迭代器中的每个子代在reactjs中都应具有唯一的“键”道具 - Each child in an array or iterator should have a unique “key” prop in reactjs 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具 - Warning :Each child in an array or iterator should have a unique “key” prop 警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具 - Warning: Each Child in an Array or Iterator Should Have a Unique “Key” Prop 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 - ReactJS - Warning: Each child in a list should have a unique "key" prop. - ReactJS 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 li中已添加的关键道具 - Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li 持久警告:“即使已经设置了密钥,数组或迭代器中的每个子节点都应该具有唯一的&#39;密钥&#39;prop” - Persistent Warning: “Each child in an array or iterator should have a unique 'key' prop” even if key is already set 列表中的每个孩子都应该有一个唯一的“钥匙”道具 Reactjs - Each child in a list should have a unique "key" prop Reactjs ReactJs-列表中的每个孩子应该有一个唯一的“关键”道具 - ReactJs - Each child in a list should have a unique “key” prop 有一种方法可以解决“警告:列表中的每个孩子都应该有一个唯一的“密钥”? - there is a way to fix "Warning: Each child in a list should have a unique "key"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM