简体   繁体   English

Ionic + React.js:警告:列表中的每个孩子都应该有一个唯一的“key”道具

[英]Ionic + React.js: Warning: Each child in a list should have a unique “key” prop

I am utilizing firebase for the database and Ionic + React for mobile app.我将 firebase 用于数据库,将 Ionic + React 用于移动应用程序。 I already converted my firebase data into array but when I want to map the values.我已经将我的 firebase 数据转换为数组,但是当我想要 map 值时。 It tells me that it should have a unique key, but I already put a unique key in my return function in the element.它告诉我它应该有一个唯一键,但我已经在元素中的返回 function 中放置了一个唯一键。 Can someone tell me what did I do wrong?有人能告诉我我做错了什么吗? Thanks.谢谢。

Here is my code in converting objects to array这是我将对象转换为数组的代码

const Tab2: React.FC = () => {
  const [doctors, setDoctor] = React.useState([]);

  useIonViewWillEnter(()=>{
    console.log('Enter')
    firebase.database().ref('users').orderByChild('type').equalTo('doctor').on('value',(snapshot)=>{
          setDoctor(Object.keys(snapshot.val()).map(key => ({ [key]: snapshot.val()[key] })))
        })
    console.log(doctors)

  })

And in my return而在我的回报

        <IonList>
        {doctors.map(elem => {
            return(
              <IonItem key={elem['uid']}>
              <IonLabel>
                <IonText className="font-weight: bold;">
                <h3>{elem['name']}</h3>
                </IonText>
                <h4>{elem['speciality']}</h4>
                <h4>{elem['email']}</h4>
              </IonLabel>
              <br></br>
            </IonItem> 
            )
          })}
        </IonList>

What I got is Warning: Each child in a list should have a unique "key" prop.我得到的是Warning: Each child in a list should have a unique "key" prop.

My firebase structure我的firebase结构

在此处输入图像描述

Update: the console.log(doctors) will only output an empty array like this [] and i dont know why.更新: console.log(doctors)只会 output 像这样的空数组[]我不知道为什么。 I already put it in a method before the component enter.在组件进入之前,我已经把它放在了一个方法中。

Note: thats just a warning, not an error.注意:这只是一个警告,而不是错误。

This is the right approach.这是正确的做法。

<IonList>
  {doctors.map((elem, index) => {
    // index has to come from the second parameter from map
    
    return (
      <IonItem key={index}>
        <IonLabel>
          <IonText className="font-weight: bold;">
            <h3>{elem["name"]}</h3>
          </IonText>
          <h4>{elem["speciality"]}</h4>
          <h4>{elem["email"]}</h4>
        </IonLabel>
        <br></br>
      </IonItem>
    );
  })}
</IonList>;

The index must be placed in the first/parent div element.索引必须放在第一个/父 div 元素中。 In this case it will be <IonItem>在这种情况下,它将是<IonItem>

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Also as per you're not getting the data.另外,根据您没有获取数据。 Adjust your code like so.像这样调整你的代码。 You just have to create a new arr, and move through snapshot to get a key.您只需要创建一个新的 arr,然后通过快照来获取密钥。

pushing the object with the respective key to a new arr, so it can be iterable without specific key names such as eA9w5ynhqN9iT7kKDBt2Ane9VF3将带有相应密钥的 object 推送到新的 arr,因此它可以在没有特定密钥名称的情况下进行迭代,例如eA9w5ynhqN9iT7kKDBt2Ane9VF3

useIonViewWillEnter(() => {
  console.log("Enter");
  let newArr = [];
  firebase
    .database()
    .ref("users")
    .orderByChild("type")
    .equalTo("doctor")
    .on("value", (snapshot) => {
      let key;
      snapshot.forEach((childSnap) => {
        key = childSnap.key; // im just getting the key
        const snapVal = snapshot.val(); // getting the object
        newArr.push(snapVal[key]); 
      });
      setDoctor(newArr);

      // console.log(doctors)
    });
});

try adding the key prop to each child as well as each element inside children and it will work.尝试将key prop 添加到每个孩子以及孩子中的每个元素,它会起作用。

your code should look like this:您的代码应如下所示:

<IonList>
    {doctors.map(elem => {
        return(
          <IonItem key={elem['uid']}>
          <IonLabel key={"lbl-" + elem['uid']}>
            <IonText key={"txt-"+elem['uid']}className="font-weight: bold;">
            <h3 key={"name-"+elem['uid']}>{elem['name']}</h3>
            </IonText>
            <h4 key={"speciality-"+elem['uid']}>{elem['speciality']}</h4>
            <h4 key={"email-"+elem['uid']}>{elem['email']}</h4>
          </IonLabel>
          <br></br>
        </IonItem> 
        )
      })}
</IonList>

if you noticed i added lbl-, txt- before the key and that to avoid another warning: "Warning: flattenChildren(...): Encountered two children with the same key,如果您注意到我在密钥之前添加了 lbl-, txt- 以避免另一个警告:“警告:flattenChildren(...): Encountered two children with the same key,

暂无
暂无

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

相关问题 React.js:警告:列表中的每个子项都应在 Next.js 中具有唯一的“键”道具 - React.js: Warning: Each child in a list should have a unique "key" prop in Next.js 警告控制台:列表中的每个子项都应该在代码 react.js 的表中具有唯一的“键”属性 - Warning console : Each child in a list should have a unique “key” prop in table in code react.js 警告:列表中的每个子项在 React.js 中都应该有一个唯一的“key”道具 - Warning: Each child in a list should have a unique “key” prop in React.js 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning: Each child in a list should have a unique "key" prop React.js - 列表中的每个孩子都应该有一个唯一的“key”道具 - React.js - Each child in a list should have a unique "key" prop React.js,列表中的每个孩子都应该有一个唯一的“关键”道具。 如果已经给出密钥,如何解决 - React.js, Each child in a list should have a unique "key" prop. how to fix if key is already given 警告:列表中的每个孩子都应该有一个唯一的“key”道具,即使它有唯一的 key React - Warning: Each child in a list should have a unique “key” prop even tho it has unique key React 反应警告:列表中的每个孩子都应该有一个唯一的“钥匙”道具,当钥匙被提供并且是唯一的 - React Warning: Each child in a list should have a unique "key" prop when key is provided and unique 反应警告消除警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning eliminate Warning: Each child in a list should have a unique "key" prop 像这样的警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - warning like : Each child in a list should have a unique "key" prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM