Skip to main content

Local State

In a React application, local state is an object that represents variables that are specific to a particular component. It is used to store data that is needed for the internal logic of the component, such as form values or toggle states, and is not needed by any other component.

To use local state in a React component, you will need to use the useState hook, which is a function that returns an array with two elements: the current state value, and a function for updating the state value.

Here's an example of how to use the useState hook to create a component with a local state value called "count":

import React, { useState } from 'react';

const Example = () => {
const [count, setCount] = useState(0);

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

In this example, the useState hook is called with an initial value of 0 for the state. This creates a state value called "count" with a value of 0, and a function called "setCount" for updating the value of "count".

The component renders a button and a paragraph element, which display the current value of "count". The button has an onClick event handler that calls the "setCount" function when the button is clicked, which updates the value of "count" by 1.

You can also use the useState hook to create multiple state values in a single component. Here's an example of how to create two state values, "name" and "age", in a component:

import React, { useState } from 'react';

const Example = () => {
const [name, setName] = useState('John');
const [age, setAge] = useState(30);

return (
<div>
<p>Name: {name}</p>
<p>Age: {age}</p>
<button onClick={() => setName('Mary')}>
Change name
</button>
<button onClick={() => setAge(age + 1)}>
Increase age
</button>
</div>
);
}

In this example, the useState hook is called with initial values of 'John' for the "name" state and 30 for the "age" state. The component renders two paragraph elements and two buttons, which display the current values of "name" and "age". The buttons have onClick event handlers that call the "setName" and "setAge" functions when clicked, which update the values of "name" and "age" respectively.

It's important to note that the useState hook should only be called from within the body of a function component, and not from inside a loop, a condition, or an event handler.

Using local state in a React component can help you create dynamic and interactive components that can change their output in response to user interactions or other events. It's a powerful tool for building modern web applications with React.