admin管理员组文章数量:1395744
I have this in the parent ponent.
<RefreshSelect
isMulti
cacheOptions
id='a'
key = 'a'
ponents={makeAnimated()}
options={this.state['filter']['provider']}
onChange={this.handleChangeProvider}
/>
<RefreshSelect
isMulti
cacheOptions
id='b'
key='b'
ponents={makeAnimated()}
options={this.state['filter']['provider']}
onChange={this.handleChangeProvider}
/>
In the definition of this child ponent, I have the constructor that updates default values depending on the id/key.
export default class RefreshSelect extends Component {
constructor(props) {
super(props);
// console.log(props)
// console.log(props.key)
if (props.key==''){
this.state = { value: [{ value: 'all', label: 'all', color: '#666666' }] };
}else if ( props.key=='a') {
this.state = { value: [{ value: '123', label: '123', color: '#666666' },
{ value: '234', label: '234', color: '#666666' },
{ value: '456', label: '456', color: '#666666' }] };
}else {
this.state = { value: [{ value: 'nvnvnvnvn', label: 'ksksksksks', color: '#666666' }] };
}
}
render() {
console.log(this.props)
return (
<Select
isMulti
defaultValue={this.state.value}
options={this.props.options}
ponents={makeAnimated()}
placeholder={this.props.label}
onChange={this.props.onChange}
/>
);
}
}
First, it looks like I'm not able to assign the key and access it in the constructor. Why is that? Secondly, what is the difference between id and key? Which one should I use and when?
I have this in the parent ponent.
<RefreshSelect
isMulti
cacheOptions
id='a'
key = 'a'
ponents={makeAnimated()}
options={this.state['filter']['provider']}
onChange={this.handleChangeProvider}
/>
<RefreshSelect
isMulti
cacheOptions
id='b'
key='b'
ponents={makeAnimated()}
options={this.state['filter']['provider']}
onChange={this.handleChangeProvider}
/>
In the definition of this child ponent, I have the constructor that updates default values depending on the id/key.
export default class RefreshSelect extends Component {
constructor(props) {
super(props);
// console.log(props)
// console.log(props.key)
if (props.key==''){
this.state = { value: [{ value: 'all', label: 'all', color: '#666666' }] };
}else if ( props.key=='a') {
this.state = { value: [{ value: '123', label: '123', color: '#666666' },
{ value: '234', label: '234', color: '#666666' },
{ value: '456', label: '456', color: '#666666' }] };
}else {
this.state = { value: [{ value: 'nvnvnvnvn', label: 'ksksksksks', color: '#666666' }] };
}
}
render() {
console.log(this.props)
return (
<Select
isMulti
defaultValue={this.state.value}
options={this.props.options}
ponents={makeAnimated()}
placeholder={this.props.label}
onChange={this.props.onChange}
/>
);
}
}
First, it looks like I'm not able to assign the key and access it in the constructor. Why is that? Secondly, what is the difference between id and key? Which one should I use and when?
Share Improve this question asked Nov 30, 2020 at 0:12 FocusFocus 1,0132 gold badges12 silver badges26 bronze badges1 Answer
Reset to default 4key
is a special prop
that helps React identify which items have changed, are added, or are removed inside lists. Keys should be given to the elements inside an array of ponents to give the elements a stable identity.
Array.from({ length: 5 }).fill(0).map((_,index) => <span key={index} />)
id
probably refers to the HTML attribute and it's going to be spreaded inside the original DOM element implemented by Select
Also you probably don't want to use index as key
本文标签: javascriptWhat is the difference between key and id in React componentStack Overflow
版权声明:本文标题:javascript - What is the difference between key and id in React component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744638622a2616979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论