when running my reducer dispatch method here:
function reducer(state, action) {
switch (action.type) {
case "updateBox":
return state.map((input, index) => {
input.map((data, NextIndex)=>{
if (action.NextIndex === NextIndex) { data.comment = action.text } else {data.comment = data.comment}
})
});
the deve tools shows that regardless of if the action.Nextindex = NextIndex the data.comment will always be changed to the action.text.
other code:
let object = {
comment:""
}
let box = [object, object]
let list = [box, box]
const [listState, listDispatch] = useReducer(reducer, list);
{ listState.map((data, index)=> (
{data.map((input, NextIndex)=>(
<TextField value={data[NextIndex].comment}
onChange={(e)=>{listDispatch({type:"updateBox", text:e.target.value, field:"comment", NextIndex, index})}}
multiline fullWidth variant="outlined"/>
)})}
sandbox link
https://codesandbox.io/s/charming-sid-wdn0h?file=/src/App.js