So, when using Context API and useReducer hook, initial state and dispatch function are defined, a Context is created and then a value is returned.
return (
<Context.Provider value={state, dispatch}>
{props.children}
</Context.Provider>
);
Then at some place it's used like this:
dispatch({ type: SET_ERROR, error: errorMessage });
That's a lot of writing each time when dispatch is called at different places (at least to me). I came up with a system where I define a function in the Context file like this:
const setError = (errorMessage) => {
dispatch({ type: SET_ERROR, error: errorMessage });
};
...and then I return setError (and other functions that I create) through the value instead of dispatch and call it where necessary like this:
setError(errorMessage);
It works the same. I perceive it as destructuring dispatch function and simplifying return value for later calls. My question is if this is a good practice? Does it influence performance, re-renders etc?