简体   繁体   English

index.js:2178警告:数组或迭代器中的每个子代都应具有唯一的“键”属性。 -reactjs

[英]index.js:2178 Warning: Each child in an array or iterator should have a unique “key” prop. - reactjs

import React from 'react';
import { Jumbotron, Button } from 'reactstrap';
import axios from 'axios';
import './App.css'


 export default class Form extends React.Component{
constructor(){
super();
this.state={

    fname:'',
    lname:''
};
}
handleChange = event => {
    this.setState({name:event.target.value});;
}

handleSubmit = event =>{
    event.preventDefault();

    const fname = this.state;
    const lname = this.state;

    axios.post('http://37692ae9.ngrok.io/api/addDocs',{fname,lname})
    .then( res => {
        console.log(res);
        console.log(res.data);
    })
    .catch((error) =>{
        alert(error);
    })
}

render(){
    return(
        <div>
        <div className="container">
            <Jumbotron fluid>
                <form onSubmit={this.handleSubmit}>
                <label>
                    firstName:
                    <input type="text" name="fname" onChange={this.handleChange} />
                    </label>
                    <label>
                    lastName:
                    <input type="text" name="lname" onChange={this.handleChange} />
                    </label>
                    <Button type="submit">add</Button>
                    </form>
                    </Jumbotron>
                    </div>

                </div>
    )
}
 }

please help me to post the two data in the url using axios whenever i was trying to send its not going.. 每当我尝试发送不成功的邮件时,请帮助我使用axios在网址中发布这两个数据。

the bug is Warning: Each child in an array or iterator should have a unique "key" prop. 该错误是警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。

i am very new to reactjs so please help me to solve this problem and make my code to execute 我对reactjs很陌生,所以请帮助我解决这个问题并使我的代码执行

When you iterate through an array and render something for each element in React, you need to assign a 'key' property to each element. 当您遍历数组并为React中的每个元素渲染某些内容时,您需要为每个元素分配一个'key'属性。 This helps React's virtual DOM keep track of the changes that take place. 这有助于React的虚拟DOM跟踪发生的更改。 Luckily, this is a pretty simple task. 幸运的是,这是一个非常简单的任务。

Say you had an array of names: const names = ['tom', 'jerry', 'frank', 'lilly'] 假设您有一个名称数组: const names = ['tom', 'jerry', 'frank', 'lilly']

to iterate through these and render a paragraph tag for each name without React complaining you would do this in your return method of your component: 要遍历这些变量并为每个名称呈现一个段落标签,而无需React抱怨,您可以在组件的return方法中执行此操作:

return (
 {
   names.map((name, index) => <p key={index}>{name}</p>
 }
)

You create a key for each element you are rendering in the list. 您为列表中要渲染的每个元素创建一个键。 It can be anything so long as it is unique. 只要是唯一的,它就可以。 So if you wanted to, you could have the names be the key, but it might no be good practice if you have duplicate values in your array. 因此,如果您愿意,可以将名称作为关键字,但是如果数组中有重复的值,则可能不是一个好习惯。

its help me 它帮助我

{
    items.map((post, index)  => (
        <PostItem key={index} {...post} />
    ))
}

暂无
暂无

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

相关问题 反应错误index.js:1452警告:数组或迭代器中的每个子代都应具有唯一的“键”属性 - React error index.js:1452 Warning: Each child in an array or iterator should have a unique “key” prop 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 li中已添加的关键道具 - Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li 数组或迭代器中的每个子代都应具有唯一的“键”道具。 - Each child in an array or iterator should have a unique “key” prop. Reactjs-数组或迭代器中的每个子代都应具有唯一的“键”道具。 如何动态地做到这一点? - Reactjs - Each child in an array or iterator should have a unique “key” prop. How to do that dynamically? 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 钥匙已经存在 - Warning: Each child in an array or iterator should have a unique “key” prop. Keys are already present 不知道为什么我会收到“警告:数组或迭代器中的每个子项都应该有一个唯一的“键”道具。” - Not sure why I am getting "Warning: Each child in an array or iterator should have a unique "key" prop." 警告:数组或迭代器中的每个子元素都应该有一个唯一的“key”属性。 反应两个按钮功能冲突 - Warning: Each child in an array or iterator should have a unique "key" prop. React two button function conflict 警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具。 检查 `ListView` 的渲染方法 - Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView` 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查“ MovieResults”的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of `MovieResults` “警告:”的简单解决方案:数组或迭代器中的每个子代都应具有唯一的“键”道具。 - Simple solution for “Warning: Each child in an array or iterator should have a unique ”key“ prop.”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM