700 B
700 B
title, tags, expertise, author, firstSeen, lastUpdated
| title | tags | expertise | author | firstSeen | lastUpdated |
|---|---|---|---|---|---|
| React useIsomporphicEffect hook | hooks,effect | beginner | chalarangelo | 2021-09-29T05:00:00-04:00 | 2021-10-13T19:29:39+02:00 |
Eesolves to useEffect() on the server and useLayoutEffect() on the client.
- Use
typeofto check if theWindowobject is defined. If it is, return theuseLayoutEffect(). Otherwise returnuseEffect().
const useIsomorphicEffect =
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
const MyApp = () => {
useIsomorphicEffect(() => {
window.console.log('Hello');
}, []);
return null;
};
ReactDOM.render(<MyApp />, document.getElementById('root'));