Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (161).NET Core  (29).NET MAUI  (64)Angular  (74)ASP.NET  (50)ASP.NET Core  (79)ASP.NET MVC  (88)Azure  (36)Blazor  (152)DocIO  (23)Essential JS 2  (85)Essential Studio  (173)File Formats  (28)Flutter  (123)JavaScript  (187)Microsoft  (113)PDF  (67)React  (62)Succinctly series  (128)Syncfusion  (610)TypeScript  (31)Uno Platform  (3)UWP  (4)Vue  (39)Webinar  (35)Windows Forms  (59)WinUI  (52)WPF  (138)Xamarin  (155)XlsIO  (25)Other CategoriesBarcode  (5)BI  (29)Bold BI  (5)Build conference  (6)Business intelligence  (54)Button  (4)C#  (119)Chart  (57)Cloud  (12)Company  (445)Dashboard  (8)Data Science  (3)Data Validation  (6)DataGrid  (55)Development  (398)Doc  (7)DockingManager  (1)eBook  (96)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (23)Extensions  (18)File Manager  (4)Gantt  (11)Gauge  (11)Git  (4)Grid  (28)HTML  (11)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (312)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (30)Performance  (9)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (7)Road Map  (10)Scheduler  (36)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (22)Solution Services  (3)Spreadsheet  (11)SQL  (8)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (92)UI  (224)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (23)Visual Studio Code  (17)Web  (374)What's new  (213)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Understanding React’s useEffect and useState Hooks

Understanding React’s useEffect and useState Hooks

React first introduced Hooks in version 16.8, and they have changed the way we write React code ever since. Hooks enable you to extract stateful logic from a functional component, allowing it to be reused and tested independently.

useState and useEffect are two of React’s most useful Hooks. Therefore, a thorough understanding of them is essential for React developers. This blog will assist you in comprehending both of these Hooks.

useState Hook

Before the useState Hook, state management in React could only be done through class components. The useState Hook in functional components serves the same purpose as this.state/this.setState in the class components.

Consider the Counter class in the following example.

class Counter extends React.Component {
  constructor(props) {
    this.state = {
      count: 0
    };
  }

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Click me
        </button>
      </div>
    );
  }
}

In this example, the count is initially set to 0, and on a button click, the count is updated by calling this.setState().

Now, let’s look at doing the same thing with the React useState Hook.

import React, { useState } from 'react';

const Counter = () => {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

As shown in the example, we must first import the useState Hook from React. Following that, we call the useState Hook from within our Counter function. The React useState Hook returns the current state and the function that updates it, which are count and setCount in our example.

Note: Remember that variables normally disappear when a function exits, but React keeps state variables, so the count variable will be preserved.

Using the useState Hook, you can declare any type of state variable. The Hook can also be used multiple times in a single component.

Syncfusion React UI components are the developers’ choice to build user-friendly web applications. You deserve them too.

useEffect Hook

When we update a state, there can be side effects that occur in parallel with each change. Data fetch requests, direct DOM manipulations, and the use of timer functions such as setTimeout() are examples of side effects. We can use the React useEffect Hook to perform side effects in function components.

Previously, these side effects were achieved using lifecycle methods like componentDidMount(), componentDidUpdate(), and componentWillUnmount(). The useEffect Hook is a combination of all these methods. It accepts a callback function that is invoked whenever a render occurs.

Take a look at the following example to see how we can use lifecycle methods to achieve the side effects.

class Time extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }
componentDidMount() {
    setInterval(
      () => this.tick(),
      1000
    );
  }
tick() {
    this.setState({
      date: new Date()
    });
  }
render() {
    return (
      <div><h2>{this.state.date.toLocaleTimeString()}</h2></div>
    );
  }
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Time />);

In this example class component, we use the lifecycle methods to set the current time. Now, let’s consider the following example to see how the useEffect Hook can be used to replace the componentDidMount() method.

import React, { useState, useEffect } from "react";
function Time() {
  const [date, setDate] = useState(new Date());
  useEffect(() => {
    setInterval(() => {
      setDate(new Date());
    }, 1000);
  }, []);
  return <div>Time: {date.toLocaleTimeString()}</div>;
}

You can see how much easier it is to implement using Hooks rather than lifecycle methods. You can play with the code here.

In React components, there are two types of side effects: those that do not require cleanup and those that do. In the previous example, no cleanup was required. Setting up a subscription, closing a socket, and clearing timers are a few common examples of side effects that require cleanup.

So, let’s see how we can use useEffect with cleanup.

A to Z about Syncfusion’s versatile React components and their feature set.

useEffect with cleanup

Cleanup in useEffect is important to prevent memory leaks.

If the useEffect() callback returns a function, useEffect() considers this to be an effective cleanup. Take a look at the following example.

import { useEffect } from 'react';
function LogMessage({ message }) {
  useEffect(() => {
    const log = setInterval(() => {
      console.log(message);
    }, 1000);
    return () => {
      clearInterval(log);
    };
  }, [message]);
  return <div>logging to console "{message}"</div>;
}

In the previous example, the cleanup function clears the interval. Rather than being called after the initial rendering, the cleanup function is called before invoking the next side-effect callback. It cleans up the previous side effect and then executes the current side effect.

If no cleanup is performed, there will be multiple console logs when only one updated log is required. Therefore, make sure to use the cleanup function in such scenarios.

Skipping effects

By default, the useEffect Hook runs after the first render as well as after each update. However, you might need to skip applying an effect in situations like when certain values haven’t changed since the last re-render.

You can easily skip applying effects by passing an array as a second argument to the useEffect Hook. As arguments, you can pass props or state values. When an argument is passed, the useEffect Hook will be executed after the initial render and only when the argument values change.

Consider the following example, in which the effect is only invoked if the variable count is updated.

useEffect(() => {
  console.log(count);
}, [count]);

If you want to run and clean up the effect only once, you can pass an empty array ([]) as the second argument.

Practical use cases of Hooks

Now, let’s look at some real-world apps for the React useState and useEffect Hooks.

You can fetch API data only when the component mounts by using the React useState and useEffect Hooks. It is not permitted to use async directly for the useEffect function. Instead, you can define and then invoke an async function within the useEffect function.

See the possibilities for yourself with live demos of Syncfusion React components.

Refer to the following code example.

import { useEffect, useState } from 'react';
function FetchItems() {
  const [items, setItems] = useState([]);
  useEffect(() => {
    async function fetchItems() {
      const response = await fetch('/items');
      const fetchedItems = await response.json(response);
      setItems(fetchedItems);
    }
    fetchItems();
  }, []);
  return (
    <div>
      {items.map(name => <div>{name}</div>)}
    </div>
  );
}

You can also use the React useState and useEffect Hooks to display a loader while fetching data. Here’s the example code for that.

const Statistics = () => {
  const [stats, setStats] = useState([]);
  const [loading, setLoading] = useState(true);
  useEffect(() => {
    const getStats = async () => {
      const stats = await getData();
      setStats(stats);
      setLoading(false);
    };
    getStats();
  }, []);

Conclusion

In this article, we discussed how to use two of React’s most essential Hooks: useState and useEffect. If we know the correct usage, they can be used to write cleaner, reusable code. I hope this article helps you understand how these two Hooks work and when should we use them.

Thank you for reading!

The Syncfusion React JS suite offers over 70 high-performance, lightweight, modular, and responsive UI components in a single package. It’s the only suite you’ll ever need to construct a complete application.

If you have any questions or comments, you can contact us through our support forums,  support portal, or feedback portal. As always, we are happy to assist you!

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top