Reputation: 31
I have two reducers, one with the name of Month and the other with Loading
export const Month = (state = '', action) =>
action.type === 'selectedMonth' ? (state = action.payload) : '';
Second one,
export const isLoading = (state = false, action) => {
switch (action.type) {
case 'setLoading':
return !state;
case 'removeLoading':
return false;
default:
return state;
}
};
I'm using dispatch inside the useEffect multiple times for the different actions. At first call i-e dispatch(actions.setMonths())
works fine but when I recall the dispatch for the different action i-e dispatch(actions.setLoading())
the store gets updated but the state of the month is set to the initial state.
dispatcher calls,
const dispatch = useDispatch();
useEffect(() => {
const fetchData = async () => {
dispatch(actions.setLoading());
const res = await getData('users');
dispatch(actions.removeLoading());
};
//any code
dispatch(actions.selectedMonth("argument"));
fetchData();
}, [months, dispatch]);
More Details,
Upvotes: 0
Views: 359
Reputation: 2422
Your Month
reducer returns ''
if the type of the action is not selectedMonth
. So when the action is setLoading
or removeLoading
, the month gets setback to an empty string
change your Month
reducer to
export const Month = (state = '', action) =>
action.type === 'selectedMonth' ? action.payload : state;
Upvotes: 2