i'm calling two dispatch's, but one resets the state of other.
See original GitHub issuei’m calling two dispatch’s, but one reset’s do state of the other, how can i call more than one dispatch’s of without reset any state? what am i doing wrong?
My customHook:
export type Action =
| { type: 'START_FETCH_PAYLOADS' }
| {
type: 'FINISH_FETCH_PAYLOADS';
microMonthlyPayloads: [];
macroMonthlyPayloads: [];
microDailyPayloads: [];
macroDailyPayloads: [];
selectedDevicePayload: [];
}
| { type: 'ERROR_FETCH_PAYLOADS' };
export const usePayloadsReducer: Reducer<State, Action> = (state, action) => {
switch (action.type) {
case 'START_FETCH_PAYLOADS':
return {
...state,
loading: true,
};
case 'FINISH_FETCH_PAYLOADS':
return {
...state,
loading: false,
microMonthlyPayloads: action.microMonthlyPayloads,
macroMonthlyPayloads: action.macroMonthlyPayloads,
microDailyPayloads: action.microDailyPayloads,
macroDailyPayloads: action.macroDailyPayloads,
selectedDevicePayload: action.selectedDevicePayload,
};
case 'ERROR_FETCH_PAYLOADS':
return {
...state,
loading: false,
};
default:
throw new Error('unknown action type');
}
};
const initialState: State = {
microMonthlyPayloads: [],
macroMonthlyPayloads: [],
microDailyPayloads: [],
macroDailyPayloads: [],
selectedDevicePayload: [],
loading: true,
};
const asyncActionHandlers: AsyncActionHandlers<Reducer<State, Action>, AsyncAction> = {
FETCH_PAYLOADS:
({ dispatch }) =>
async (action) => {
dispatch({ type: 'START_FETCH_PAYLOADS' });
try {
switch (action.filter) {
case 'PAYLOADS_OF_SELECTED_DEVICE': {
//fetching and formatting data...
dispatch({
type: 'FINISH_FETCH_PAYLOADS',
...state,
selectedDevicePayload: formattedDevice
});
break;
case 'MICRO_AND_MACRO_BY_MONTH': {
//fetching and formatting data...
dispatch({
type: 'FINISH_FETCH_PAYLOADS',
...state,
microMonthlyPayloads: monthlyMicroResponse.data[0],
macroMonthlyPayloads: monthlyMacroResponse.data[0],
});
break;
}
default:
return state;
}
} catch (e) {
dispatch({ type: 'ERROR_FETCH_PAYLOADS' });
}
},
on my component:
const getPayloads = useCallback(() => {
dispatch({
type: 'FETCH_PAYLOADS',
filter: 'PAYLOADS_OF_SELECTED_DEVICE',
});
dispatch({
type: 'FETCH_PAYLOADS',
filter: 'MICRO_AND_MACRO_BY_MONTH',
});
}, []);
useEffect(() => {
getPayloads();
}, [getPayloads]);
Issue Analytics
- State:
- Created a year ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
redux dispatch resets other states to default - Stack Overflow
The problem is that in both your reducers, you're returning the default state in the default case. This means that every non handled...
Read more >useReducer's dispatch should return a promise ... - GitHub
So dispatch could just return a Promise<void> : ... do I know when it has finished affecting the state? with setState I have...
Read more >How To Manage State in React with Redux - DigitalOcean
Step 1 — Setting Up a Store · Step 2 — Creating Actions and Reducers · Step 3 — Dispatching Changes in a...
Read more >FAQ | Frequently asked questions - GovernmentJobs
I have more than one account. How can I merge them? Is it possible to delete or reset my account? Do I need...
Read more >How to Manage State in a React App – With Hooks, Redux ...
To sum it up, we just need: A reducer, that is the function that will consolidate all possible state changes; A dispatch function,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
ok @dai-shi, got it! thank you!!
It will overwrite. Note that this library doesn’t do anything special in terms of its behavior. It’s more or less a syntax sugar. It works exactly like you put async logic in your callback (where you call
dispatch
).