Eesolves to useEffect()
on the server and useLayoutEffect()
on the client.
typeof
to check if the Window
object is defined. If it is, return the useLayoutEffect()
. Otherwise return useEffect()
.const useIsomorphicEffect =
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
const MyApp = () => {
useIsomorphicEffect(() => {
window.console.log('Hello');
}, []);
return null;
};
ReactDOM.render(<MyApp />, document.getElementById('root'));
React, Hooks
Checks if the code is running on the browser or the server.
React, Hooks
Checks if the client is online or offline.
React, Hooks
Observes visibility changes for a given element.