본문 바로가기

Frontend/React

Hooks - useEffect (Catching Changes in a Component)

반응형

The useEffect is a hook that catches changes in a component. It works similarly to event or setInterval and using this we can update the component whenever there is a change.

Setting Up a Project

Creating a React App

 

React

React is the most papular framework followed by Angular and Vue. React, like Angular uses components to build an application. However, React does not come with native features like Angular. Instead, it uses other libraries which makes React compact and sma

jin-co.tistory.com

How to Use It?

Import the hook and initialize it as shown below.

We can add the targets in an array after the curly braces. So whatever you put in the array will be tracked to catch the changes (Like the event and setInterval combined).

Examples

Creating a Context

Add a context and create a status to track

Adding Components

Add a component and initialize the useEffect hook with the status brought in from the context.

Go to the app component and add the context provider to use the context and inside there add the component.

Testing

Run the app and click the button. Whenever the button is clicked you will see the change is tracked.

In this writing, we have seen how to use the useEffect hook.


References

Traversy Media - YouTube

 

Traversy Media

Traversy Media features the best online web development and programming tutorials for all of the latest web technologies from the building blocks of HTML, CSS & JavaScript to frontend frameworks like React and Vue to backend technologies like Node.js, Pyth

www.youtube.com

 

 

728x90
반응형