I was going through couple of examples from official website. I found below example which sets the button values based on click.
With reference to above example, I used array map and tried to set button value of perticular button.
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
render() {
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
<button key={number} onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
);
return (
{listItems}
);
}
}
ReactDOM.render(
<Toggle />,
document.getElementById('root')
);
As per above modified code, I've given unique key but still I could see all buttons are setting at a time.
Why its happening? How I can achieve its result. Why I'm not able to set unique identification?