616 B
616 B
title, tags, firstSeen
| title | tags | firstSeen |
|---|---|---|
| useIsomporphicEffect | hooks,effect,beginner | 2021-09-29T05:00:00-04:00 |
Eesolves to useEffect() on the server and useLayoutEffect() on the client.
- Use
typeofto check if thewindowobject is defined and return theuseLayoutEffect()if it is, oruseEffect()otherwise.
const useIsomorphicEffect =
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
const MyApp = () => {
useIsomorphicEffect(() => {
window.console.log('Hello');
}, []);
return null;
};
ReactDOM.render(<MyApp />, document.getElementById('root'));