React Chat App Home Page Static Component

0
Posted at July 2, 2020 362 Views . 1 Saved .

HomePage > index.js

HomePage > style.css

India
Coding is an art and I love art
1 Comments
Saurabh Behra
Hii When I am fetching data inside useEffect why api is being called infinite times though i am adding dependency to it. Dependency is the value of useState through which i am updating the value.
Rizwan Khan
useEffect dependency array is very sensitive, change in dependency array value force the component to Re-render. useEffect(() => { //updating var1 here totally bad idea. it will triger infinite rerendering }, [var1]);
Saurabh Behra
Exactly useEffect(()=>{ fetching data from api and SetState(array of objects) },[array of object]) So what could be the better approach?
Rizwan Khan
So you want to fetch data from api. A simple solution would be just leave the dependency array empty const [data, setData] = useState([]); useEffect(async () => { const response = await fetch(api); const jsonResponse = await response.json(); setData(jsonResponse); }, []);
Saurabh Behra
This is correct but i have to re render my dom on any change in state. That is why i am passing dependency. And dependency is creating infinite render.