React Redux initState

Asked

Viewed 77 times

0

It’s common when you work with Redux set an initial state, example:

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, 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, how to inform on Redux the incial state of these fields?

2 answers

2

The Redux status of the application ("application status") stores information required for the use of 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 such as 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 in the 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 also do this in mapStateToProps.

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.