No Preview

Rizwan Khan

Posted on 2 July 2020

React Chat App Home Page Static Component

HomePage > index.js

HomePage > style.css

0

559

1

No Preview

Rizwan Khan

India

Coding is an art and I love art

Explore more similar Post

1 Comments

Saurabh Behra

29 May 2021

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.

1
REPLY

Rizwan Khan

31 May 2021

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]);

0
REPLY

Saurabh Behra

31 May 2021

Exactly useEffect(()=>{ fetching data from api and SetState(array of objects) },[array of object]) So what could be the better approach?

0
REPLY

Rizwan Khan

31 May 2021

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); }, []);

0
REPLY

Saurabh Behra

31 May 2021

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.

0
REPLY