Reputation: 1052
On button press, I send a dispatch to a reducer that does the following:
export default function (state, action) {
case 'UPDATE_DATA':
const res = state;
res['list'] = [...res['list'], action.payload];
return res;
I access the data with the useSelector hook.
const select = useSelector(state => state);
renderData(select.userInfo.list);
However, it doesn't seem to trigger an update. What is wrong with this code?
(I tried to make this question as minimal as possible. So if you need more code context, just ask.)
Upvotes: 0
Views: 23
Reputation: 10382
here const res = state;
you are passing the same state
reference to res
. this way will be mutating the original object. when you return res
redux will compare res reference with state reference and will realize they are the same and not work as expected.
you need to create a copy out of state first:
const res = { ...state };
this way you won't mutate the original object and your redux state will update properly.
Upvotes: 1