From 5233b1cf6e17f3a664760cf67692b6962c7149ba Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Fri, 3 Sep 2021 11:28:21 +0300 Subject: [PATCH] Add useUpdate --- snippets/useUpdate.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 snippets/useUpdate.md 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 ( + <> +
Time: {Date.now()}
+ + + ); +}; + +ReactDOM.render(, document.getElementById('root')); +```