我使用的是反应在页面上上安装两个相同的组件,React.js混淆了“两个孩子使用相同的密钥”
var SelectBox = React.createClass({
getDefaultProps: function() {
return {
value: []
};
},
getInitialState: function() {
return {checked: this.props.value,count: 1};
},
handleClick: function() {
var opt = this.state.checked;
opt.push({id: this.state.count, name: this.state.count});
this.setState({checked: opt, count: this.state.count + 1});
},
render: function() {
var that = this;
var options = this.state.checked.map(item => <option key={'checked-' + that.props.name + item.id} value={item.id}>{item.name}</option>);
return (
<div>
<select multiple={true}>
{options}
</select>
<button type="button" onClick={this.handleClick}>add</button>
</div>
);
}
});
React.render(
<SelectBox name='one'/>,
document.getElementById('one')
);
React.render(
<SelectBox name='two'/>,
document.getElementById('two')
);
然后点击“一”的按钮,这是正常的,但是当我点击'two'按钮时,'two'中的一些出现在'two'中,为什么?这让我感到困惑。控制台显示:
Warning: flattenChildren(...): Encountered two children with the same key, `.$checked-two1`. Child keys must be unique; when two children share a key, only the first child will be used.
只是做了一些变化
var a = [{id: 5, name: 5}];
React.render(
<SelectBox name='one' value={a}/>,
document.getElementById('one')
);
它正常工作。 发生了什么?有没有什么错误或它的错误?
谢谢你你的答案,你的意思是没有明确价值的所有组件将是相同的实例?在这种情况下,我应该为每个组件提供明确的价值? – alchemist 2015-04-01 06:09:33
没有,只是在你的情况,以及如何小提琴设置。 – 2015-04-01 07:08:19
@limelights所以我会如何解决这个问题?我有同样的问题,最有可能是因为你所描述的。用例是它的一个烤面包机,当一个新的通知进来我不印字的阵列到新添加到数组的开头(这样他们就可以从底部加载),所呈现的先前通知获得不同的道具,因为他们现在要在不同的地方渲染......但通知本身不会改变。 – 2015-12-10 02:35:29