I'm trying to change state, with values from array. Example:
const [state, setState] = useState({});
const test = [1, 2, 3];
test.map((item, i) => {
setState({ ...state, [`item-${i}`]: item });
});
Current state is:
item-2: 3
What I want to achieve is:
item-0: 1,
item-1: 2,
item-2: 3
I've tried to do it in several ways (all looking similar), but the effect is the same :/ does anyone knows how to resolve it?
Thanks!
const newState = { ...oldState }; test.forEach((item, i) => newState['item-' + i] = item); setState(newState);item-N, because you're essentially duplicating the functionality of an array :)