I found the state.elements was changed in console, even I do not dispatch yet. What is the reason?
const initialState = { elements: [['apple','banana'],['rabbit','cat']] };
function reducer(state, action) {
switch (action.type) {
case "increment":
return { count: state.count + 1 };
case "decrement":
return { count: state.count - 1 };
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = React.useReducer(reducer, initialState);
const changeList=()=>{
const elementsArray = Array.from(state.elements);
elementsArray[0][0]='Tiger'
}
return (
<>
Count: {state.elements}
<button onClick={changeList}>Change List without dispatch</button>
</>
);
}