Last active
May 1, 2020 13:31
-
-
Save willprice76/2f54d5bc57acb1c9680605b6ec26df6d to your computer and use it in GitHub Desktop.
Managing React component state with useReducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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