19
loading...
This website collects cookies to deliver better user experience
useNetworkState
.const App = () => {
const [isOnline, setIsOnline] = useState(window.navigator.onLine);
useEffect(() => {
const handleOnline = () => {
setIsOnline(true);
};
const handleOffline = () => {
setIsOnline(false);
};
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, []);
return (
<div>
<h1>My Awesome App</h1>
<p>
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Culpa
provident tenetur molestias fugiat expedita quaerat dolores dignissimos
dicta, error amet reiciendis voluptates delectus perspiciatis dolorum
saepe, sunt, similique vitae illo.
</p>
{!isOnline && (
<div className="toast">
You are offline. Please check your connectivity and try again.
</div>
)}
</div>
);
};
useEffect
hook. Our goal is to define a useNetworkState
hook that will abstract this logic inside a custom hook, that is reusable over the entire app to listen for network state changes. This will also reduce the code inside our App
component, that could quickly get longer and longer if we add some other logic (click listeners, form submission, keyboard listeners...).const isOnline = useNetworkState()
useEffect
hook of our App
component. At the end, the hook will look like this:const useNetworkState = () => {
const [isOnline, setIsOnline] = useBoolean(window.navigator.onLine);
useEffect(() => {
window.addEventListener('online', setIsOnline.on);
window.addEventListener('offline', setIsOnline.off);
return () => {
window.removeEventListener('online', setIsOnline.on);
window.removeEventListener('offline', setIsOnline.off);
};
}, []);
return isOnline;
};
useBoolean
? This hook doesn't exist... 🤨useBoolean
hook.Note: if you don't want to use the useBoolean
hook, you can be satisfied with the native useState
one, and useNetworkState
would be the following:
const useNetworkState = () => {
const [isOnline, setIsOnline] = useState(window.navigator.onLine);
useEffect(() => {
const handleOnline = () => {
setIsOnline(true);
};
const handleOffline = () => {
setIsOnline(false);
};
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, []);
return isOnline;
};
App
component, where we can drastically simplify the code (see by yourself):const App = () => {
const isOnline = useNetworkState()
return (
<div>
<h1>My Awesome App</h1>
<p>
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Culpa
provident tenetur molestias fugiat expedita quaerat dolores dignissimos
dicta, error amet reiciendis voluptates delectus perspiciatis dolorum
saepe, sunt, similique vitae illo.
</p>
{!isOnline && (
<div className="toast">
You are offline. Please check your connectivity and try again.
</div>
)}
</div>
);
};