React Redux initState

0

It is common when you work with redux to define an initial state, eg

const initState = {
    pagInicial:1,
    pagAtual:1,
    maxLinks:2,
    maxReg:5
}

So far so good. Now and when I need an initial state, starting with some parameters and calculated fields, for example:

const initState = {
    pagInicial:1,
    totReg:pegar total registros
    pagAtual:1,
    maxLinks:2,
    maxReg:5,    
    totLinks:Math.ceil(totReg/maxReg),
    regInit:((maxReg*pagAtual​)-maxReg),
    regFim:(maxReg*pagAtual​))
}

TotReg , TotLinks , regInit , regFim , / i> the initial state of these fields?

    
asked by anonymous 30.11.2017 / 03:45

1 answer

1

The application's Redux state ("application state") stores the information needed to use the application. The initial state is determined for convenience and to detail which properties will be used by the application. Normally, we use initial values like empty string, null, empty list, etc. to indicate that the data has not yet been obtained.

It is not recommended to store computed values in the application state if that value is obtained by an operation on data that is already stored in the state.

You already have access to the data via Redux, so do the calculations on the component itself using the existing properties. You can do this also in mapStateToProps.

    
30.11.2017 / 23:17