useState() Hook in Next JS - React Hooks

useState() Hook in Next JS - React Hooks

3 min read · 429 words · Shared December 30, 2021 by

Article Summary: Learn what useState is and learn how to use useState in Next JS by coding some examples.

Hooks Overview

React Hooks were introducted React 16.8. They allow you to create stateful components without writing a class. For more info, check out an article we wrote on the useEffect hook.

What is useState Hook?

The useState hook lets you manage state throughout your application. Let's take a look at an example.

What does calling useState do?

Simply put, a state variable persists the state throughout the lifecycle of your application instead of dissapearing when the function is exited.

useState Hook Example

import React, { useState } from 'react'

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0)

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  )
}

The example above is from the React docs. As you can see, we are storing the count inside a state variable.

We can access the count from the count variable and we can change the count using the setCount function.

More Complex Example

You can also store more than a simple number in a state variable. Let's look at some examples.

const [count, setCount] = useState(0);
const [name, setName] = useState('Max');
const [items, setItems] = useState([1, 2, 3]);
const [bool, setBool] = useState(true);
const [obj, setObj] = useState({ a: 1, b: 2 });
const [null, setNull] = useState(null);

As you can see, we can store ints, strings, arrays, booleans, objects, nulls, custom objects, and more inside of a useState variable.

Uses of useState

A cool use of useState is to dynamically change a css value. Maybe as the user scrolls, we want to change the font-size of the text.

const [fontSize, setFontSize] = useState('10px')
const handleScroll = () => {
    setFontSize(window.scrollY + "px")
}

...

<p style={{ fontSize: fontSize }}>
    Hello World
</p>

The p tag above will increase in size as the user scrolls down the page!

Enjoyed this article on useState? Explore more React hooks!

Edit on GitHub

Related Tags

    #react

Related Posts

Fix For - Error: Hydration Failed Because The Initial UI Does Not Match What Was Rendered on The Server · November 3, 2023
useContext() Hook in Next.js - React Hooks · October 8, 2023
Advanced Chakra UI · August 29, 2021
Add Comments To React/Next.js Website · May 1, 2021

On This Page

Hooks Overview

What is `useState` Hook?

What does calling useState do?

`useState` Hook Example

More Complex Example

Uses of `useState`

View Related Posts

November 3, 2023

nextjs-light.png

Fix For - Error: Hydration Failed Because The Initial UI Does Not Match What Was Rendered on The Server

October 8, 2023

react.png

useContext() Hook in Next.js - React Hooks

August 29, 2021

chakra-ui.png

Advanced Chakra UI

May 1, 2021

utterances.png

Add Comments To React/Next.js Website


Loading author data...

    Legal

    Terms

    Disclaimer

    Privacy Policy


    Carlson Technologies Logo© Copyright 2021 - 2024, Carlson Technologies LLC. All Rights Reserved.