Very basic code sample of a list:
class List extends React.Component {
render() {
const listComponent = this.props.numbers.map((number) =>
<Item key={ number.toString() } value={ number } />,
);
return (
<div>
<button onClick={ () => this.setState({ test: 1 })}>Re-render list</button>
{ listComponent }
</div>
);
}
}
An here is the item:
class Item extends React.Component {
render() {
return (
<div>{ this.props.value + ', rendered time:' + new Date().getTime() }</div>
);
}
}
When I click the button, the state is updated so the List component is re-rendered.
However, if my understanding is correct, the items should not be re-rendered since the key item has not changed. But it does re-render since the timestamp is updated.
Can someone explain me why?