diff --git a/snippets/useUpdate.md b/snippets/useUpdate.md new file mode 100644 index 000000000..2c33ef493 --- /dev/null +++ b/snippets/useUpdate.md @@ -0,0 +1,31 @@ +--- +title: useUpdate +tags: components,reducer,beginner +firstSeen: 2021-09-24T05:00:00-04:00 +--- + +Forces the component to re-render when called. + +- Use the `useReducer()` hook that creates a new object every time it's updated and return its dispatch. + +```jsx +const useUpdate = () => { + const [, update] = React.useReducer(() => ({})); + return update; +}; +``` + +```jsx +const MyApp = () => { + const update = useUpdate(); + + return ( + <> +