Menu

Your Custom Hooks Tear in React 18. `useSyncExternalStore` Fixes It.
๐Ÿ“ฐ

Your Custom Hooks Tear in React 18. `useSyncExternalStore` Fixes It.

DEV Community: javascriptยทParsa Jiravandยท4 days ago
#FYlKX8cg
#devtojavascript#devto#dev#react#window
Reading 0:00
15s threshold

The header says "You're offline." Three pixels below it, the save button is still clickable, sending requests like nothing's wrong. Same navigator.onLine value. Same render. Two different answers on screen at the same time.

You didn't write a new bug. You wrote the hook everyone writes.

function useOnlineStatus () { const [ online , setOnline ] = useState ( navigator . onLine );

useEffect (() => { const update = () => setOnline ( navigator . onLine ); window . addEventListener ( ' online ' , update ); window . addEventListener ( ' offline ' , update ); return () => { window . removeEventListener ( ' online ' , update ); window . removeEventListener ( ' offline ' , update ); }; }, []);

return online ; }

Enter fullscreen mode

Exit fullscreen mode

It worked in every app you shipped before React 18.โ€ฆ

Continue reading โ€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.