I have a useState in nextjs that has values like this:
const [sampleData, setSampleData] = useState({
value1: ''
value2: ''
value3: []
});
If I want to update the state of value1 for instance while keeping the rest of the state, I would do something like this:
setSampleData({...sampleData, value1: 'new value'});
This works. However, if I want to update the state of value3 which is an array and still keep the previous array contents it has before, how do I achieve this? Doing something like this would replace the entire state of value3 array with the new set of arrays:
setSampleData({...sampleData, value3: [new array]});
Is it possible or should I simply use another format entirely such as useReducer
hook?