user5809117
user5809117

Reputation:

Is it Okay to store functions and instances of Class in redux store?

I'd like to know what can be stored and what can't be stored in redux store as well as why can't be stored for what can't be stored.

I heard that I should not store instances of Class since they are not easily serializable(...?)

What about functions like this?

function person(firstName, lastName) {
    let _firstName = firstName;
    let _lastName = lastName;

    return {
        fullName: `${_firstName} ${_lastName}`
    }
}

Also, I'm wondering if I'm not supposed to store instances of Class, why redux recommends storing immutable.js object into redux store although immutable.js objects are instances of Class with many methods?

Update

What about instances of a class extending immutable.js Record?

Upvotes: 5

Views: 4382

Answers (1)

palsrealm
palsrealm

Reputation: 5243

The Redux store is supposed to be the 'single source of truth' for the application. It is where all the state of the application should reside. Any type that can be stored as state of a Component can be stored in Redux.

Your question seems to consist of 2 main parts.

  1. Should Components be stored in Redux? . ( I am assuming that by 'instance of Class' you mean the React Components) The short answer is no. A Component can be divided into 2 parts, the markup (and associated logic in methods) and the state of the component. The markup and associated logic of the Component will never change once the Component has been created. It is the state of the Component that keeps on changing and which this state is applied to the Component, the markup of the Component changes accordingly. If we are able to get the state of the Component, we can apply it to the Component and get the necessary markup. Thus, storing the Component in Redux will give us no benefit as we would only load it once and it will never change.

    As far as instances of Immutable.js objects are concerned, they are generally objects which represent the state of a Component. So they are prime examples of what should be stored in a Redux store.

    Edit: Regarding Immutable.js Records, they are immutable versions of a normal json object whose shape cannot be tampered with. They are used to represent the state of a Component and are immutable by design; which would make them ideal for storing in Redux as we are not supposed to mutate the state stored in Redux.

  2. Should Functions be stored in Redux? The answer again is no. A function is pure logic which is applied on the input parameters to get an output. For the same input, a function would ideally give the same output. Thus storing such functions will be of no use, as they would never change, once they have been created.

    The function in your question will always give the same output for the same input. So, storing the function would not be recommended; instead storing the input values in Redux and applying them to the function to get the output , would be recommended.

Upvotes: 4

Related Questions