Skip to content

Instantly share code, notes, and snippets.

@willprice76
Last active May 1, 2020 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willprice76/2f54d5bc57acb1c9680605b6ec26df6d to your computer and use it in GitHub Desktop.
Save willprice76/2f54d5bc57acb1c9680605b6ec26df6d to your computer and use it in GitHub Desktop.
Managing React component state with useReducer
//component state
const [{ recipes, isLoading, loadingFailed, loadingError }, dispatch] = useReducer(
createLoadRecipesReducer(), { isLoading: true, loadingFailed: false }
);
//load data
useEffect(() => {
dispatch({ type: "busy" });
api.getRecipes().then(response => {
dispatch({ type: "success", result: response.data });
}).catch((error: string) => {
dispatch({ type: "error", error });
});
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment