const [allrestro, setallrestro] = useState([]);
const [filteredrestaurant, setfilteredrestaurant] = useState([]);
const search = useState("");
[searchValue, setsearchValue] = search;
useEffect(() => {
const getRestaurants = async () => {
const data = await fetch("https://www.swiggy.com/dapi/restaurants/list/v5?lat=30.9188442&lng=75.8148262&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING");
const json = await data.json();
setallrestro(json?.data?.cards[5]?.card?.card?.gridElements?.infoWithStyle?.restaurants);
setfilteredrestaurant(json?.data?.cards[5]?.card?.card?.gridElements?.infoWithStyle?.restaurants);
setTimeout(() => {
}, 10000);
}
getRestaurants();
}, []);
const isOnline = useOnline();
if (!isOnline) {
return <h1>You are offline</h1>
}
It is working fine But when I do this
const Body = () => {
const [allrestro, setallrestro] = useState([]);
const [filteredrestaurant, setfilteredrestaurant] = useState([]);
const search = useState("");
[searchValue, setsearchValue] = search;
const isOnline = useOnline();
if (!isOnline) {
return <h1>You are offline</h1>
}
useEffect(() => {
const getRestaurants = async () => {
const data = await fetch("https://www.swiggy.com/dapi/restaurants/list/v5?lat=30.9188442&lng=75.8148262&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING");
const json = await data.json();
setallrestro(json?.data?.cards[5]?.card?.card?.gridElements?.infoWithStyle?.restaurants);
setfilteredrestaurant(json?.data?.cards[5]?.card?.card?.gridElements?.infoWithStyle?.restaurants);
setTimeout(() => {
}, 10000);
}
getRestaurants();
}, []);
It is not working!
Is there any problem if I call useOnline above useEffect
Here is the UseOnline Hook
import { useState,useEffect } from "react";
const useOnline=()=>{
const [state,usestate]=useState(true);
console.log("outside component component")
useEffect(()=>{
console.log("outside component");
window.addEventListener("online",()=>{usestate(true)});
window.addEventListener("offline",()=>{usestate(false)});
setTimeout(() => {
console.log("useOnline useffect")
}, 5000);
},[])
return state;
}
export default useOnline;
Can you explain also how hooks is called if there are multiple hooks inside a component?
The cycle of component runs seperately for custom hooks or is it bind to the calling fucntion?
2
Answers
Calling a custom hook in a component that calls
useEffect
is no different than callinguseEffect
in the component body. Custom hooks are meant to encapsulate logic, but otherwise work exactly the same.An important rule in React is that all hooks must be called at the top level, so you should never call them inside conditions or after early returns. So for example the rule is violated if you call:
Since if
!isOnline
, the seconduseEffect
might not be called. To fix it, simply moveuseEffect
above the early return.The
useOnline
custom hook works by callinguseEffect
to listen for theonline
andoffline
events. When the user goes online, the state is set totrue
. When the user goes offline, the state is set tofalse
.The
useEffect
hook is called in the following order:useEffect
hooks are called in the order they are defined.This means that the
useEffect
hook in theBody
component will be called after theuseOnline
custom hook.The reason why your code is not working is because the
useOnline
custom hook is returning the initial state oftrue
before theuseEffect
hook in theBody
component has a chance to run. This means that theuseEffect
hook in theBody
component will never be called, because theisOnline
state variable will always betrue
.To fix this, you can move the
useOnline
custom hook into theuseEffect
hook in theBody
component, like this:The cycle of the component run separately for custom hooks or is it bind to the calling function
Custom hooks are essentially functions that return React state and other values. They are not separate components. When a custom hook is used in a component, the code in the custom hook is executed within the context of the calling component. This means that the custom hook has access to the component’s state and other values.