Making API Calls with React Hooks

Harsh Makadia
Bits and Pieces
Published in
5 min readAug 5, 2019

--

With the new updates coming up in the React library, it's indeed impossible to use all the new React features in your application. It’s been 6 months since the official release of React Hooks which was released with React 16.8.0 (February 6, 2019)

This article will help you take the baby steps in using React Hooks, it will explain all the basic approach which you can take to make the most out of this beautiful feature.

React Hooks [ Icon Credit — wanicon, freepik ]

Let’s start with Quick Introduction to React Hooks.

Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don’t work in classes — they let you use React without classes.

💡 Note: If you’re building custom hooks in React, you could use a tool like Bit to publish, version, and reuse them across all of your projects with a simple npm i @bit/your-username/your-custom-hooks.

Learn more here:

useState 😄

useState is a Hook, We call it inside a function component when we want to add some local state to it. The good thing about this is that the state will be preserved during re-rendering.

useState returns a pair: the current state value and a function that lets you update your component. Calling a function will work similarly to this.setState where it will update the values of the state, except it will not merge old and new state.

useEffect 😄

The Effect Hook, useEffect adds the ability to perform side effects from a function component.

The purpose of useEffect is similar to the purpose of Lifecycle methods in the class component like componentDidMount , componentDidUpdate and componentWillUnMount

You can also decide when to re-render. Consider below the example where we have passed a count array after the useEffect.

useEffect(() => {
document.title = `You clicked ${count} times`;
}, [count]); // Only re-run the effect if count changes

Let’s consider if the count value is 60 and if the component re-renders with the count value being unchanged i.e. 60, React will compare the previous render value and decide whether to call effect or not. If values are different then only the effect is called. Now that’s a way to increase performance and avoid unnecessary calls. 💯 🚀

If there are multiple items in the array, React will re-run the effect even if just one of them is different.

Converting Class Component into a Functional Component with Hooks ⚖️

Let’s look at the example of how we can get the same behavior as a class component in a function component using Hooks.

Example: Consider an example where you need to make API calls and fetch the data and populate in our component and clicking on the load more button would fetch more data from the server.

Until the Release of React 16.8.0(Hooks), it wasn't possible to achieve these using functional components as lifecycle methods aren’t accessible in the functional component and it wasn’t possible to manage the state inside a functional component.

For making API calls we will use Github APIs https://developer.github.com/v3/search/#search-commits

Here is what a typical React code looks like for both ordinary class component and functional component using Hooks.

API call code [ Icon Credit — Roundicons ]

Whenever API calls are involved we need multiple state values —

  • Holding that data that is to be rendered
  • Page count to make API call
  • Loading state (show loading screen/component until the data is received from server)
  • Error state (show error message when something goes wrong while fetching data)

Thus above image with Class component and the functional component does the same thing of loading the commits from the Github. Thus this simple example will help you understand how easy it is to start using hook into your application. With hooks, you can use write code neatly and sort.

API Calls with React Hooks

Here are the rules you should keep in mind while working with React Hooks

  1. Don’t try to convert the old code written in class components into Hooks. However, it is recommended you can try using Hooks in the new implementation
  2. useState and useEffect are the two new concepts which you should know to master Hooks
  3. Only call Hooks at the top level. Don’t call Hooks inside loops, conditions, or nested functions.
  4. Only call Hooks from React function components. Don’t call Hooks from regular JavaScript functions.

Thus this is how React Hooks can be useful in making API calls, sometimes we have to convert a functional component into a class component only because of not being able to manage the state inside the functional component.

Reference
- https://reactjs.org/

Happy Learning 💻 😄

Build React Apps with reusable components, just like Lego

Bit’s open-source tool help 250,000+ devs to build apps with components.

Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.

Learn more

Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:

Micro-Frontends

Design System

Code-Sharing and reuse

Monorepo

--

--

I talk about JavaScript, Web Development, No-code to help you stay ultra-modern. See you on Twitter — https://twitter.com/MakadiaHarsh