import React from 'react'
import ReactDOM from 'react-dom'
class App extends React.Component{
constructor(props) {
super(props)
this.state = {
list: [{id: 1,val: 'aa'}, {id: 2, val: 'bb'}, {id: 3, val: 'cc'}]
}
}
click() {
this.state.list.reverse()
this.setState({})
}
render() {
return (
<ul>
<div onClick={this.click.bind(this)}>reverse</div>
{
this.state.list.map(function(item, index) {
return (
<Li key={item.id} val={item.val}></Li>
)
}.bind(this))
}
</ul>
)
}
}
class Li extends React.Component{
constructor(props) {
super(props)
}
componentDidMount() {
console.log('===did===')
}
componentWillUpdate(nextProps, nextState) {
console.log('===mount====')
}
render() {
return (
<li>
{this.props.val}
<input type="text"></input>
</li>
)
}
}
ReactDOM.render(<App/>, document.getElementById('app'))
when I set key as item.id
, and I set three input tags a
, b
, c
;
when I click reverse, the component Li will mount, input will reverse
when I change key as index
, when I click reverse, the component Li update, and the input tag will not change,
I want to know how does it happen? Does Anyone have found out how key works?