Can I use multiple states?

066

Yes, you can use multiple state variables in a functional component by calling the useState hook multiple times. Each call to useState creates a separate piece of state. Here's an example:

import React from 'react';

const MyComponent = () => {
    const [count, setCount] = React.useState(0);
    const [name, setName] = React.useState('John Doe');

    return (
        <div>
            <p>Count: {count}</p>
            <button onClick={() => setCount(count + 1)}>Increment</button>
            <p>Name: {name}</p>
            <button onClick={() => setName('Jane Doe')}>Change Name</button>
        </div>
    );
};

export default MyComponent;

In this example, there are two state variables: count and name, each managed independently.

0 Comments

no data
Be the first to share your comment!