1

I am experimenting with the useReducer hook in React

here is my following code in sandbox:

https://codesandbox.io/s/usedispatch-users-c58t4?file=/src/App.js

I create a user object with an empty array called interests. The array is initially empty and I have a form to add interests into the array per every instance of a user. This can be found in my 'User' component

in my reducer function with the case of 'ADD_INTEREST' I'm having a hard time understanding why I have to re-assign all the current user values with my action.payload before I add an interest into my interests array. I have been messing around for a bit and I can't seem to get it to work without having those values there. I know I'm missing something somewhere. Any feedback would be much appreciated. Thanks

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Ben Shekhtman
  • 385
  • 5
  • 15
  • You don't need to re-assign those properties manually; spread syntax will add those properties in the newly created object. Here's a [demo](https://codesandbox.io/s/usedispatch-users-forked-yvt95?file=/src/App.js) of how you can add a new interest. Changes made in `User.js` and `App.js` – Yousaf Aug 11 '21 at 04:52

0 Answers0