question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

i'm calling two dispatch's, but one resets the state of other.

See original GitHub issue

i’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:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eduardomarrcommented, Sep 13, 2022

ok @dai-shi, got it! thank you!!

1reaction
dai-shicommented, Sep 13, 2022

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found