Day 8:React Hooks (Part 2)

After a thorough introduction to React Hooks and the useState hook on day 7, we learned about the useEffect hook in React on the eighth day under the mentorship of Bikash Karki .

What is useEffect hook in React?

The useEffect in ReactJS is used to handle the side effects such as fetching data and updating DOM.

Why use useEffect hook?

useEffect hook is used to handle side effects in functional components, such as fetching data, updating the DOM, and setting up subscriptions or timers. It is used to mimic the lifecycle methods of class-based components. The motivation behind the introduction of useEffect Hook is to eliminate the side effects of using class-based components. For example, tasks like updating the DOM, fetching data from API end-points, and setting up subscriptions or timers, can lead to unwarranted side effects. Since the render method is too quick to produce a side-effect, we use life cycle methods to observe the side effects.

How does it work?

We call useEffect with a callback function that contains the side effect logic. By default, this function runs after every render of the component. We can optionally provide a dependency array as the second argument. The effect will only run again if any of the values in the dependency array change.

Importing useEffect hook

To import the useEffect hook, we write the following code at the top level of the component

import { useEffect } from "react"        

Structure of useEffect hook

The useEffect hook syntax accepts two arguments where the second argument is optional.

//useEffect(callback/function, dependency)
useEffect(
    () => {
        // execute side effect (function)
    },
    // optional dependency array
    [
        // 0 or more entries
    ] 
)        

FUNCTION: contains the code to be executed when useEffect triggers.

DEPENDENCY: is an optional parameter, useEffect triggers when the given dependency is changed.

What is the useEffect cleanup function?

As the name implies, useEffect cleanup is a function in the useEffect Hook that allows us to tidy up our code before our component unmounts. When our code runs and reruns for every render, useEffect also cleans itself up using the cleanup function. The cleanup function is commonly used to cancel all active subscriptions and async requests.

useEffect(() => {
        effect
        return () => {
            cleanup
        }
    }, [input])        

Fetching Data from an API using Axios

Axios is a third library package that we can add to our program to retrieve information from an API. Because Axios is used in both web browsers and server-side JavaScript, it is useful for a wide range of tasks.

We install Axios package as follows:

npm i axios        

Then, we import it in our project

import axios from 'axios'        

In the following way, we can fetch the data from the API.

axios.get('https://example.com') 
.then(response => {console.log(response.data)}) 
.catch(error => {console.log(error)})        

Now, the data is ready to be used in various React components.

要查看或添加评论,请登录

Aastha Sharma的更多文章

  • ExpressJS

    ExpressJS

    What is ExpressJS? Express.js is a Node.

  • Day 13: Forms in React (Formik and Yup)

    Day 13: Forms in React (Formik and Yup)

    On the thirteenth day, I learned about forms in React, the Fragment tag, and react-icons in the react application…

  • Day 12: localStorage

    Day 12: localStorage

    It can be infuriating to close a webpage while filling out a form accidentally. We lose all the data already filled and…

  • Day 11: Layout, Offset, and the useParams Hook

    Day 11: Layout, Offset, and the useParams Hook

    Layout and Offset In React, "offset" and "limit" are commonly used when dealing with paginated data. These terms are…

  • Day 10: Using API data and Pagination in React

    Day 10: Using API data and Pagination in React

    APIs in React APIs (Application Programming Interfaces), in React, are used to fetch data from external sources, such…

  • Day 9: Environment Variables and Toastify in React

    Day 9: Environment Variables and Toastify in React

    What are Environment Variables? Environment variables in React serve as dynamic configurations for our application…

  • Day 7: React Hooks

    Day 7: React Hooks

    On the seventh day of learning React, we learned about React hooks, their use, and their applications. React Components…

  • Day 6: Dynamic React Components using Props

    Day 6: Dynamic React Components using Props

    React utilizes a component-based architecture that enables developers to create reusable and modular UI elements. A…

  • Day 5: React Layouts, Outlets, and Links

    Day 5: React Layouts, Outlets, and Links

    On the fifth day of learning react, we grabbed on concepts like layouts, outlets, and links in React. They are…

  • Day 4: React-Router-DOM and BootStrap

    Day 4: React-Router-DOM and BootStrap

    On the fourth day of learning React, we learned about installing Bootstrap and React router DOM, all thanks to our…

社区洞察

其他会员也浏览了