How you guys handle initialstate of your loading state in redux reducer? is it always true? The problem is you have to always set it to false in every single case later in the reducer.
Like so
const initState = {
loading: true,
data: null,
error: null
}
export function global(state=initState, action) {
switch(action.type){
case FETCHING:
return {
...state,
loading: true
}
case FETCH_SUCESS:
return {
...state,
data: action.payload,
loading: false
}
case FETCH_FAILED:
return {
...state,
error: action.payload.error,
loading: false
}
default:
return state
}
}
If it's set to false you will have this problem
class AClassName extends Component {
constructor(props){
super(props)
props.callAnApi()
}
//assume in reducer your loading initialstate is false
render() {
this.props.something && return <Redirect to={`/app/${this.props.something}`} />
return !this.props.loading && <SomethingElse { ...this.props } />
}
}
How to return nothing if it's finished loading. Above problem is that SomethingElse component will still got rendered once which is not what I want. So what's your initialState of loading?