在Redux中获取错误:“0”;遇到两个具有相同密钥的子项;。如何解决此错误?

在Redux中获取错误:“0”;遇到两个具有相同密钥的子项;。如何解决此错误?,redux,react-redux,Redux,React Redux,我正在学习Redux。在同一个容器中,我想使用MapStateTops和mapDispatchToProps。如果我将它们分离到不同的容器中,我可以使其正常工作而不会出现错误,但是如果我尝试将它们都放在同一个容器中,出于某种原因,它会产生以下错误: "bundle.js:2296 Warning: flattenChildren(...): Encountered two children with the same key, `.$1`. Child keys must be unique

我正在学习Redux。在同一个容器中,我想使用MapStateTops和mapDispatchToProps。如果我将它们分离到不同的容器中,我可以使其正常工作而不会出现错误,但是如果我尝试将它们都放在同一个容器中,出于某种原因,它会产生以下错误:

"bundle.js:2296 Warning: flattenChildren(...): 
Encountered two children with the same key, `.$1`. 
Child keys must be unique; when two children share a key, 
only the first child will be used."
在同一容器中同时使用mapStateToProps和mapDispatchToProps可以吗?一切正常,但我仍然在控制台上看到错误,我想弄清楚它的确切含义

这是我的集装箱。(我必须采取行动:抓取宇航员和showNotification):

import React,{Component}来自'React';
从'react redux'导入{connect};
从“redux”导入{bindActionCreators};
从“../actions/index”导入{FetchSpautor};
从“../actions/index”导入{showNotification};
类搜索栏扩展组件{
建造师(道具){
超级(道具);
此.state={
文本:“”
}
}
显示搜索(e){
this.setState({text:e.target.value})
}
表格提交(e){
e、 预防默认值();
this.props.fetch宇航员(this.state.text);
this.setState({text:'});
this.props.showNotification();
}
render(){
返回(
此.formSubmit(e)}>
this.displaySearch(e)}
/>
搜寻
{this.props.notification}
);
}
}
函数mapstatetops({notification}){
返回{通知};
}
功能图DispatchToprops(调度){
返回bindActionCreators({FetchSpautor,showNotification},dispatch);
}
导出默认连接(mapStateToProps、mapDispatchToProps)(搜索栏);
当然可以使用MapStateTops和mapDispatchToProps,事实上这是非常值得推荐的

至于警告,这是一个react JSX警告:您在同一级别包含两个div,但没有键。它们必须有一个“key”参数,并且必须不同

render() {
    return (
        <div>
            <div key='1'>
                <form onSubmit={(e) => this.formSubmit(e)}>
                    <input
                        value = { this.state.text }
                        onChange={(e) => this.displaySearch(e) }
                    />
                    <button type="submit">Search</button>
                </form>
            </div>
            <div key='2'>
                <h1>{this.props.notification}</h1>
            </div>
        </div>
    );
}
render(){
返回(
此.formSubmit(e)}>
this.displaySearch(e)}
/>
搜寻
{this.props.notification}
);
}

在同一个容器中同时使用mapStateToProps和mapDispatchToProps是完全可以的,您看到的错误在别处

当您传递组件列表时,应为每个组件分配唯一id。例如,如果您有代码:

const Comp = (props) => {
 const l =[1,2,3,4]
 const lc = l.map((ix) => (<p>{ix}</p>))
 return (
  <div>{lc}</div>
 )
}
const Comp=(道具)=>{
常数l=[1,2,3,4]
常量lc=l.map((ix)=>({ix}

) 返回( {lc} ) }
会给你带来和你一样的错误。要解决此问题,您应将其更改为:

const lc = l.map((ix) => (<p key={ix}>{ix}</p>))
const lc=l.map((ix)=>(

{ix}

这样每个段落都有其唯一的键

const lc = l.map((ix) => (<p key={ix}>{ix}</p>))