You need a way to delegate the right portion of the sate to the component, so if your application has a login component then the handler in the login component should be passed the portion of the state that it is responsible for.
If loading looks different for components than you could have a loading
member in your component state and set that to true or false using a function from a global utilities module or inject it from your application.
Handler of the application (action type is an array):
let utils = {
toggleLoading:(sate)=>
Object.assign(
{}
,state
,{loading:!state.loading}
)
}
switch(action.type.slice[0,1]) {
case "login":
return Object.assign(
{}
,applicationState
,{
login:
loginHandler(
applicationState.login
,Object.assign(
{}
,action
,{type:action.type.slice(1)}
)
,utils
)
}
)
}
login handler
switch(action.type.slice[0,1]) {
case "toggleLoading":
return utils.toggleLoading(state)
}
Since a component does not know how it's nested you have to pass the action type array from the application to the component and from components to sub components (action type can be added to state).
If you want to use one component to show and hide loading then the code is similar except your login component will delegate toggleLoading action to the loading component the same way as application delegates it to login.
No need to pass a utilities object in that case since there is only one loading component so you won't have duplicate implementation of how loading is set.