According to docs state of react app has to be something serializable. What about classes then?
Let's say I have a ToDo app. Each of Todo
items has properties like name
, date
etc. so far so good. Now I want to have methods on objects which are non serializable. I.e. Todo.rename()
which would rename todo and do a lot of other stuff.
As far as I understand I can have function declared somewhere and do rename(Todo)
or perhaps pass that function via props this.props.rename(Todo)
to the component.
I have 2 problems with declaring .rename()
somewhere: 1) Where? In reducer? It would be hard to find all would be instance
methods somewhere in the reducers around the app. 2) Passing this function around. Really? should I manually pass it from all the higher level components via And each time I have more methods add a ton of boilerplate to just pass it down? Or always do and hope that I only ever have one rename method for one type of object. Not Todo.rename()
Task.rename()
and Event.rename()
That seems silly to me. Object should know what can be done to it and in which way. Is not it?
What I'm missing here?
No, you should not store function references in the redux store. They are not serializable, and as you mentioned state should be serializable at all times.
Redux restricts updating the state to this method only. This strict way of updating the state ensures that the state can not be changed directly either by view or any network callback. The only way to update a state is by defining the action and then dispatching it. Remember that actions are plain JavaScript objects.
The usual way to define an action in Redux is to separately declare an action type constant and an action creator function for constructing actions of that type. The createAction helper combines these two declarations into one. It takes an action type and returns an action creator for that type.
Can I put functions, promises, or other non-serializable items in my store state? It is highly recommended that you only put plain serializable objects, arrays, and primitives into your store.
In Redux, you don't really have custom models. Your state should be plain objects (or Immutable records). They are not expected to have any custom methods.
Instead of putting methods onto the models (e.g. TodoItem.rename
) you are expected to write reducers that handle actions. That's the whole point of Redux.
// Manages single todo item function todoItem(state, action) { switch (action.type) { case 'ADD': return { name: action.name, complete: false }; case 'RENAME': return { ...state, name: action.name }; case 'TOGGLE_COMPLETE': return { ...state, complete: !state.complete }; default: return state; } } // Manages a list of todo items function todoItems(state = [], action) { switch (action.type) { case 'ADD': return [...state, todoItem(undefined, action)]; case 'REMOVE': return [ ...state.slice(0, action.index), ...state.slice(action.index + 1) ]; case 'RENAME': case 'TOGGLE_COMPLETE': return [ ...state.slice(0, action.index), todoItem(state[action.index], action), ...state.slice(action.index + 1) ]; } }
If this still doesn't make sense please read through the Redux basics tutorial because you seem to have a wrong idea about how Redux applications are structured.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With