React Projects and State management

Amarachi Nwokocha
3 min readJun 13, 2024

--

The core function of every application is its ability to manage, store, and manipulate data created within it or received from an external source.

This data can be in the form of variables, objects, booleans, etc. It can be of any data type supported by the language used. This data has to be stored, modified, and used in whatever way the application needs it.

React, a javascript framework well known for its component-based structure works with all datatypes supported by javascript.

One of the core features that makes React powerful and flexible is its ability to manage state within components.

The useState hook, introduced in React 16.8, is a fundamental tool for managing state in functional components.

This article will discuss :

  • What is useState hook?
  • How is it used in an application?
  • Best practices for using it
  • Benefits of using useState hook in your application

What is useState hook?

State in react is an object that contains real-time data used by the application.

It is a built-in React object that is used to contain data or information about the component. A component’s state can change over time; whenever it changes, the component re-renders.

The useState hook is a powerful addition to React, introduced in version 16.8. It allows the management of state within functional components without the need for class-based components.

It is important to note that one use of useState() can only be used to declare one state variable.

How to use usestate hook in React syntax

To use the usestate hook, it has to be imported at the top level of your application.

The usestate hook takes on an initial state and returns two values.

The first value contains the state and the second value is a function that updates the state the value set to the state will be taken as its initial value.

This is done by calling the usestate hook and passing the initial value you want to assign to that variable.

In the example, apple is the state setApple is the function that updates apple and 0 is apple’s initial value.

How to Update state using React useState

To update the value set by a state, the second value in the usestate array will be called and the updated value will be passed into it’ll look like this:

Now the value of apple will be equal to the value of data.

Best practices for usestate in react

While there are various tips on how best to use the usestate hook, adhering to the following tips helps you have clean reusable code

  • Declare the variable with “const”, it can’t be let or var
  • Always have a default value while initializing even if it is zero this makes it readable and also avoids runtime issues
  • To prevent unnecessary re-renders, avoid unnecessary state
  • Use multiple state variables for multiple pieces of state.

Writing cleaner and more readable code that works is the goal of every developer, usestate offers a clean way to handle data in an application to update the said data.

This powerful hook can be used in various circumstances try it out in your next project and explore what it has to offer.

Happy coding!

--

--

Amarachi Nwokocha

Hello world, I’m a tech loving transitioning Chinese teacher, That loves to have fun while writing about JavaScript.