DEV Community

Cover image for What is useState?
_Khojiakbar_
_Khojiakbar_

Posted on

What is useState?

Imagine you have a magical treasure chest (your React component) where you can store and change treasures (pieces of data). The useState is like a special spell that helps you create and manage these treasures. Every time you want to create a new treasure or change it, you use this spell.

How Does useState Work?

When you use the useState spell, it gives you two things:

1. The current value of your treasure (let's call it the treasure).
2. A magical key that can change the treasure.

Casting the useState Spell

Let's see how we can use this spell in our treasure chest.

import React, { useState } from 'react';

function TreasureChest() {
  const [treasure, setTreasure] = useState('Gold Coins');

  return (
    <div>
      <h1>My Treasure: {treasure}</h1>
      <button onClick={() => setTreasure('Diamonds')}>Change Treasure</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation:

1. Import the Spell: First, you import the useState spell from React.

2. Create Your Treasure: Inside your treasure chest (component), you cast the useState spell:

const [treasure, setTreasure] = useState('Gold Coins');
Enter fullscreen mode Exit fullscreen mode

treasure is your current treasure, starting with 'Gold Coins'.
setTreasure is the magical key to change your treasure.

3. Display the Treasure: You show your current treasure using:

<h1>My Treasure: {treasure}</h1>
Enter fullscreen mode Exit fullscreen mode

4. Change the Treasure: You create a button to change your treasure. When you click the button, it uses the magical key (setTreasure) to change the treasure to 'Diamonds':

<button onClick={() => setTreasure('Diamonds')}>Change Treasure</button>
Enter fullscreen mode Exit fullscreen mode

A Funny Example: The Magic Cookie Jar

Imagine you have a magic cookie jar that starts with one type of cookie and can magically change to another type when you say the magic words.

import React, { useState } from 'react';

function MagicCookieJar() {
  const [cookie, setCookie] = useState('Chocolate Chip');

  return (
    <div>
      <h1>Magic Cookie Jar: {cookie} Cookies</h1>
      <button onClick={() => setCookie('Oatmeal Raisin')}>Change to Oatmeal Raisin</button>
      <button onClick={() => setCookie('Peanut Butter')}>Change to Peanut Butter</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

How It Works:

  1. Start with Chocolate Chip: The jar starts with 'Chocolate Chip' cookies.
  2. Magical Buttons: There are buttons to change the cookies to 'Oatmeal Raisin' or 'Peanut Butter'.
  3. Using the Magical Key: When you click a button, it uses the setCookie magic key to change the type of cookie.

Visualizing useState:

Treasure Chest: Your component is like a treasure chest.
Treasure: The state (data) inside the chest, like 'Gold Coins' or 'Diamonds'.
Magical Key: The function (setTreasure) to change the treasure.

By using useState, you can keep track of different treasures (pieces of data) and change them whenever you want, making your React component interactive and dynamic!

In Summary:

useState is a magical spell in React that helps you create and manage pieces of data (state) inside your components. You start with a treasure, and you can change it using a magical key. This makes your components come alive with interactivity, just like a magic cookie jar that changes cookies with a spell! 🍪✨

Top comments (0)