diff --git a/src/hooks/useState.ts b/src/hooks/useState.ts index d08cdbef..7cbfa8eb 100644 --- a/src/hooks/useState.ts +++ b/src/hooks/useState.ts @@ -22,12 +22,13 @@ export default function useState( const destroyRef = React.useRef(false); const [value, setValue] = React.useState(defaultValue); - React.useEffect( - () => () => { + React.useEffect(() => { + destroyRef.current = false; + + return () => { destroyRef.current = true; - }, - [], - ); + }; + }, []); function safeSetState(updater: Updater, ignoreDestroy?: boolean) { if (ignoreDestroy && destroyRef.current) { diff --git a/tests/hooks.test.js b/tests/hooks.test.js index 0a769388..caa4f773 100644 --- a/tests/hooks.test.js +++ b/tests/hooks.test.js @@ -137,10 +137,28 @@ describe('hooks', () => { [], ); - return null; + return ( + + ); }; - const { unmount } = render(); + const { container, unmount } = render( + + + , + ); + expect(container.querySelector('button').textContent).toEqual('0'); + + // Update Value + fireEvent.click(container.querySelector('button')); + expect(container.querySelector('button').textContent).toEqual('93'); + unmount(); setTimeout(() => {