0

I have the following problem in a React Context:

    import * as React from 'react';
     
    export const Context = React.createContext();
     
    function reducer (state, action) {
        switch (action.type) {
            case 'set': {
               SomePackage.parse(action.result, {
                    options: true
                    complete: (result) => {
                        // Result is available here
                    }
                });
            }
            default: {
                throw new Error(`Unhandled action type: ${action.type}`)
            }
        }
    }
    
    export function ContextProvider ({ children }) {
        const [state, dispatch] = React.useReducer(reducer, {});
    
        return (
            <Context.Provider value={{state, dispatch}}>
                {children}
            </Context.Provider>
        )
    }

I can't really find a to return data inside of reducer from SomePackage.parse. The data that should be set as the new state is only available inside of the completecallback and is not returned in any way from SomePackage.parse Is there an elegant way to return those data?

Meatgear
  • 119
  • 1
  • 5
  • `SomePackage.parse` doesn't return a new state. Why can't you just return a new object with the updated properties? – Andy Jul 01 '21 at 12:29
  • 1
    Does this answer your question? [React useReducer async data fetch](https://stackoverflow.com/questions/53146795/react-usereducer-async-data-fetch) – Jazz Jul 01 '21 at 12:29
  • @Andy the data i want to set as the new state are only available inside of the ```complete```callback – Meatgear Jul 01 '21 at 12:46

0 Answers0