S
S
sanex33392017-02-19 22:18:16
redux
sanex3339, 2017-02-19 22:18:16

Redux, a couple of questions?

I'm picking Redux, wrote a small project (1000 lines), a couple of questions appeared.
1)
There is a state property - `type`, which can have the value `typeA`, `typeB` or `typeC` (radio buttons), as well as an object with default data of the form:

{
    typeA: {//... defaults},
    typeB: {//... defaults},
    typeC: {//... defaults}
}

Previously, this object with default data was part of the state, but now I moved it into a separate class with the `getDefaultByType(type)` method, and wherever I need to get the default data for the current `type`, I call this method.
Is this approach correct with default data?
2)
In state there are some properties that depend on the `type` property. For example, the property `price`.
For each `type` there is an array of available prices. During initialization, the 1st price from the array is placed in the `price` property (then it is already controlled by the drop-down list).
`type` is in `initialState`, but what do I do with `price` that depends on `type`?
- manually write the correct `price` for `type` in `initialState`, given that if `type` suddenly changes during initialization, will you need to change `price` as well?
- or make a side effect that will work when the state is initialized (ngrx, when the state is initialized, a special action is performed that catches the side effect) and, based on the current `type`, choose the correct `price`?
Now I'm using the 2nd option, when initializing the state, the side effect takes the value of the `type` property and performs 5 actions to fill the `type`-dependent properties in the state.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question