I receive an array of objects through sockets, I write this data into the redox store.

But! when we change something on the site (in my case it is creating a new lobby or changing any data in it) then only a small part of the data comes to us (example: UPDATE_LOBBY: { user: admin, ..., ... }

And here is the question: how to rewrite this data in redux correctly? After all, the reducer should be a pure function, in action it also looks crazy, then where? or how?

  • 3
    add code to the question please) - Ilya Paymushkin
  • @ IlyaPaymushkin Add is not a problem, just what exactly? sockets? code that comes in sockets? redux? reducer? - ceri
  • Rejuser code, it seems to me that the problem is there - Ilya Paymushkin
  • @ IlyaPaymushkin, there is no problem, there is a question - how to do it correctly. Now in reducer, I simply write through via {{state, fields: action.payload} - ceri
  • @ IlyaPaymushkin But I don’t understand how to rewrite parts of these same fields, because all the options known to me are not adequate as part of redux (I did all the rewriting in the reducer) - ceri

1 answer 1

All data in the Redux Store is transmitted only through Action. The new state of the Store is generated based on its previous state and action (Action).

A partial update involves copying from the previous state references to objects that have not changed, any changes should generate new objects. Example of a partial Store update:

 function reduce(state, action) { switch (action.type) { case "CHANGE_DATA_ENTRY": { const previosDataEntry = state.data[action.key] const changeUser = action.user ? {user: action.user} : {} const changeEmail = action.email ? {email: action.email} : {} const changeAvatarURI = action.avatarURI ? {avatar_uri: action.avatarURI} : {} return { ...state, data: { ...state.data, [action.key]: { ...previosDataEntry, ...changeUser, ...changeEmail, ...changeAvatarURI } } } } } return state }