Skip to content

Instantly share code, notes, and snippets.

@dance2die
Last active September 7, 2020 16:16
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 dance2die/59a29c7b9a6be44d59a3f2893dac5c1e to your computer and use it in GitHub Desktop.
Save dance2die/59a29c7b9a6be44d59a3f2893dac5c1e to your computer and use it in GitHub Desktop.
/**
* No longer works
* Refer to https://sung.codes/blog/2018/11/08/emulate-forceupdate-with-react-hooks/
**/
import React, { useState } from 'react';
const useForceUpdate = () => useState()[1];
const App = () => {
const forceUpdate = useForceUpdate();
console.log('rendering');
return <button onClick={forceUpdate}>Click To Render</button>;
};
export default App;
@cristinaITdeveloper
Copy link

It doesn't work for me. I get this error:
Possible Unhandled Promise Rejection (id: 7): Error: Invalid hook call. Hooks can only be called inside of the body of a function component.

@dance2die
Copy link
Author

Hi @cristinaITdeveloper.
It no longer works and I shoulda linked to the blog post that addresses it.
https://sung.codes/blog/2018/11/08/emulate-forceupdate-with-react-hooks/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment