I can't make it not work well if I use index as the key. The only way is if I mutate the array and use index as the key. But since the docs says not to mutate the state (the array), so if that's the case, I can't make it not work well, contrary to what the docs is stating. How can I show that it may break?
function App() {
const [arr, setArr] = React.useState(["A","B","C"]);
function toggleSortOrder() {
let newArr = [...arr];
newArr.reverse();
console.log(JSON.stringify(newArr));
setArr(newArr);
}
return (
<div>
<ul>
{ arr.map((e, i) => <li key={i}>{ e }</li>) }
</ul>
<button onClick={toggleSortOrder} >Toggle Sort Order</button>
</div>
)
}
ReactDOM.render(<App />, document.querySelector("#root"));
<script src="https://unpkg.com/react@16.12.0/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16.12.0/umd/react-dom.development.js" crossorigin></script>
<div id="root"></div>
I can make it break if I mutate the state, which the docs says should not be done:
function App() {
const [arr, setArr] = React.useState(["A","B","C"]);
function toggleSortOrder() {
arr.reverse();
console.log(JSON.stringify(arr));
setArr(arr);
}
return (
<div>
<ul>
{ arr.map((e, i) => <li key={i}>{ e }</li>) }
</ul>
<button onClick={toggleSortOrder} >Toggle Sort Order</button>
</div>
)
}
ReactDOM.render(<App />, document.querySelector("#root"));
<script src="https://unpkg.com/react@16.12.0/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16.12.0/umd/react-dom.development.js" crossorigin></script>
<div id="root"></div>
But I can't even break it if I mutate the state and use index as the key, if it is a class component:
class App extends React.Component {
state = { arr: ["A","B","C"] };
toggleSortOrder() {
this.state.arr.reverse();
console.log(JSON.stringify(this.state.arr));
this.setState({ arr: this.state.arr });
}
render() {
return (
<div>
<ul>
{ this.state.arr.map((e, i) => <li key={i}>{ e }</li>) }
</ul>
<button onClick={this.toggleSortOrder.bind(this)} >Toggle Sort Order</button>
</div>
);
}
}
ReactDOM.render(<App />, document.querySelector("#root"));
<script src="https://unpkg.com/react@16.12.0/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16.12.0/umd/react-dom.development.js" crossorigin></script>
<div id="root"></div>