Author avatar

Marques Woodson

Fetching Data and Updating State with Hooks

Marques Woodson

  • Dec 19, 2019
  • 8 Min read
  • Dec 19, 2019
  • 8 Min read
Web Development

Fetching Data and Updating State with Hooks

A previous guide covered how to fetch data from a REST API and how to re-render a React component with the results. The guide detailed how to do this with the componentDidMount() lifecycle method that every React class component provides. Many components, however, are not written as classes. Most likely because classes are not yet fully supported by modern browsers, and it would require the use of Babel or TypeScript to transpile into supported JavaScript. When a component is created without using a class, but instead with a normal function declaration, it is called a Functional Component. In React, a functional component does not have the same built-in lifecycle methods that classes do. They also don't have their own state, so you can't call this.setState(...). As a result, you may want to know how to fetch data and re-render the component.

Introducing Hooks

As of version 16.8, React functional components have actually been able to use their own state and their own lifecycle methods. This functionality is possible due to React's new Hooks.

Hooks are functions that are prefixed with the word use... and allow you to hook into the component's lifecycle features from functional components.

There are 10 available built-in hooks, but the two most common hooks are useState and useEffect.


According to the docs, the useState hook is similar to the this.setState() call in class components, except that it doesn't merge the old and new states together. The useState hook returns the state value and a function for updating that state:

1import React from 'react';
3function BooksList () {
4   const [books, updateBooks] = React.useState([]);

The above example shows how to use the useState hook. It declares a state variable called books and initializes it with an empty array. The array destructuring syntax might look intimidating, but it allows you to name your state variable and the updater function. That updater function allows you to update the state like this:

1function BooksList () {
2   const [books, updateBooks] = React.useState([]);
4   const handleClick = () => {
5       // update the books state property by adding a new book
6       updateBooks([...books, { name: 'A new Book', id: '...'}]);
7   }
9   return (
10   <ul>
11 => (
12        <li key={}>{}</li>  
13       ));
14   </ul>
15   );

When the above handleClick method is called, the updateBooks updater function will be called with the new array of books as its only argument. The books property is then updated and the component will re-render with the new value of books.


When you must perform side effects in a functional component, you can use the helpful useEffect hook.

The Effect Hook lets you perform side effects in function components

The useEffect hook takes two arguments, a function and a list of dependencies. If the function or the dependencies change, the function is called.

1function BooksList () {
2   const [books, updateBooks] = React.useState([]);
4   React.useEffect(function effectFunction() {
5       if (books) {
6           updateBooks([...books, { name: 'A new Book', id: '...'}]);
7       }
8   }, []); // This empty array represents an empty list of dependencies
9   ...

The above example introduces the useEffect hook with an empty dependency list. As a result, this hook will only run when the component initializes. It won't run any other time because it doesn't have any dependencies to watch.

Using an empty array provides almost the same functionality as the componentDidMount() lifecycle method from a class component.

The following example shows what happens if you do provide a value in the dependency array:

1function BooksList () {
2   const [books, updateBooks] = React.useState([]);
3   const [counter, updateCounter] = React.useState(0);
5   React.useEffect(function effectFunction() {
6       if (books) {
7           updateBooks([...books, { name: 'A new Book', id: '...'}]);
8       }
9   }, [counter]);
11   const incrementCounter = () => {
12       updateCounter(counter + 1);
13   }
14   ...

In this example, the effectFunction will be called when the component initializes, and also every time the counter variable changes.

When using the useEffect hook, it's common to compare it to all of the class component lifecycle methods. Instead, you should think of the useEffect hook in terms of how you want your state to look after certain variables change.

Some rules to keep in mind about the useEffect hook:

  • You cannot call a hook within a conditional;
  • Hooks must be called in the exact same order. Putting the useEffect inside of a conditional could break that cycle;
  • The function you pass the hook cannot be an async function, because async functions implicitly return promises, and a useEffect function either returns nothing or a cleanup function.

Bringing it Together

A common use case for which you'll need the useEffect is fetching some data from a server and updating the state with its contents. You can combine using the useEffect hook and the useState hook to accomplish this behavior. Imagine you want to fetch a list of Harry Potter books from a REST API. After the fetch, you want to display an unordered list of book titles in your component. That can look like this:

1import React from 'react';
3function BooksList () {
4   const [books, updateBooks] = React.useState([]);
6   React.useEffect(function effectFunction() {
7       fetch('')
8           .then(response => response.json())
9           .then(({ data: books }) => {
10               updateBooks(books);
11           });
12   }, []);
14   return (
15   <ul>
16 => (
17        <li key={}>{}</li>  
18       ));
19   </ul>
20   );

The above hook only runs when the component is initialized. If you're more likely to use async/await in your useEffect hook, it may look like this:

1function BooksList () {
2   const [books, updateBooks] = React.useState([]);
4   React.useEffect(function effectFunction() {
5       async function fetchBooks() {
6           const response = await fetch('');
7           const json = await response.json();
8           updateBooks(;
9       }
10       fetchBooks();
11   }, []);
13   return (
14   <ul>
15 => (
16        <li key={}>{}</li>  
17       ));
18   </ul>
19   );


Introducing the Hooks API provides a lot of functionality in your functional components. The useEffect hook allows you to handle side effects such as logging, making asynchronous calls, or setting values on local storage. The useState hook lets you give state to your functional components, which wasn't possible before unless you used a class component. A later guide will cover more hook functionality, including how to create your own custom hooks.

Please view this guide if you would like to learn how to update state and re-render a component using a class component.

Learn More

Explore these React courses from Pluralsight to continue learning: