Currently, when changing states in a component, I subscribe the state to a store which updates when a function I call finishes. I do this by:
useAirQualityStore.getState().updateAirQuality();
...
this.state = useAirQualityStore.subscribe(state => this.setState(state));
However, when I am working on a screen where I need to use multiple stores, I have more parameters and can't set the entire state. Could I subscribe certain parameters of my state to listen for changes in the stores?
My idea of this would be somehow passing in the previous state but it seems a bit messy:
useWeatherStore.subscribe(console.log, state => this.setState(prevState => {
return {
...prevState,
forecast: state.dailyForecast,
weatherIsLoading: state.forecast.isLoading
}
}));
Is there a better way to do this?