Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,109 members, 7,835,742 topics. Date: Tuesday, 21 May 2024 at 02:12 PM

Useeffect Hook In React. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Useeffect Hook In React. (239 Views)

Understanding The Concepts Of View, State, Props, Lifecycle & Component In React / Example To Set Border Radius Of An Image In React Native / Embed Youtube Video In React Native Android Using Webview Component (2) (3) (4)

(1) (Reply) (Go Down)

Useeffect Hook In React. by chukwuebuka65(m): 9:10am On Sep 22, 2022
I don’t know this might help but let me drop it here.

Some useEffect hook mistakes and how to avoid them.

What is useEffect?

UseEffect is a react hook used in function components to run side effects. Side effects are those actions which a block of code produces which modifies or reads from data outside its scope e.g changing the value of a variable declared outside it scope, making request to an api, saving data to local storage etc.

when does useEffect run?

Unlike other hooks, which run while react is excuting the components functions , useEffect runs after react has return from the function and updated the dom which allows you to then run sideEffects. UseEffect also acts as component DidUpdate and componentWillUnmount for function components .

What is useEffect cleanup?

Because useEffect runs after react updates the dom. There a possibility that a user may cause a component to unmount by clicking away from a page before useEffect finishes running (eg when making a call to an external api on a different server). In that case , when the results come back , useEffect will try to update the state of our component so we can use the data . But remember our component has unmourned, this could cause our application to behave unexpectedly. To avoid this we need to abort any request we are making or any code we are running that has not completed when our components unmounts. In the case of fetching data we can use the abort Controller of fetch or axios.

What is the dependecy array for?

An empty array causes useEffect to run once , that is on initial render and not on subsequent rerenders until the component unmounts. If you pass a variable to useEffect, then useEffect runs whenever value of that variable changes. Mind you that javascript and in turn react compare primitive data types by values and objects by reference. Meaning if you pass an object, useEffect will run repeatedly non-stop running into an infinite loop because every time the component rerender , the component recreates the variables inside it meaning the address of the previous objects in memory will likely not be same thereby causing useEffect to run into infinite loop.
So watch out for these when building your projects
Re: Useeffect Hook In React. by LikeAking: 9:30am On Sep 22, 2022
Hooks are react curses.
Re: Useeffect Hook In React. by chukwuebuka65(m): 9:35am On Sep 22, 2022
LikeAking:
Hooks are react curses.

I haven’t built anything using classes but I thought functions components are simpler than class components.
Re: Useeffect Hook In React. by LikeAking: 9:47am On Sep 22, 2022
chukwuebuka65:


I haven’t built anything using classes but I thought functions components are simpler than class components.

People are used to JS functions so function components is something we can easily relate with.

As for React hooks they are just crazy, buy very useful.
Re: Useeffect Hook In React. by chukwuebuka65(m): 9:54am On Sep 22, 2022
LikeAking:


People are used to JS functions so function components is something we can easily relate with.

As for React hooks they are just crazy, buy very useful.

Ok

(1) (Reply)

Understanding Threat Modeling In Cyber Security / This Is How To Create An Attractive Hero Header Using Html And Css ---- / Lockout-tagout Software

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 11
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.