How to use handleChange() function in react component?
Last Updated :
09 Jan, 2025
An onChange event is triggered when values are entered in the input. This fires a function handleChange(), that is used to set a new state for the input.
1. Handling Single Input
First, we have to set up the input field as a controlled component so that we have a controlled component that senses changes and updates the state accordingly.
We will learn handleChange() function with the help of a project in which we're going to use handleChange() function to display the entered input.
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app handlechangedemo
Step 2: After creating your project folder i.e. handlechangedemo, move to it using the following command:
cd handlechangedemo
Project Structure: It will look like the following.
Project StructureApp.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
JavaScript
import React, { useState } from "react";
const App = () => {
/* Initial State */
let [Name, setname] = useState('');
/* The handleChange() function to set a new state for input */
const handleChange = (event) => {
setname(event.target.value);
}
return (
/* Short-form of React.Fragement*/
<>
<form>
{ /* The handleChange() is triggered when text is entered */}
<div>
<h1>My Name is <span style={{ color: 'red' }} >
{Name}</span></h1>
<input
type="text"
value={Name}
onChange={handleChange}
placeholder="Enter a text"
/>
</div>
</form>
</>
)
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
The following will be the output if the user enters an input which is set with the help of handleChange() function as shown below:

2. Handling Multiple Input
In the above project write down the following code in the App.js file for handling multiple inputs.
JavaScript
import React, { useState } from 'react';
const App = () => {
let [Fullname, setFullname] = useState({
fname: '',
lname: ''
})
const handleChange = (event) => {
let value = event.target.value;
let name = event.target.name;
setFullname((prevalue) => {
return {
...prevalue, // Spread Operator
[name]: value
}
})
}
return (
<>
<form>
<div>
<h1>Hello <span style={{ color: 'red' }}>{Fullname.fname}</span>
<span style={{ color: 'green' }}>{Fullname.lname}</span></h1>
<input type='text' placeholder='Enter Your FirstName'
onChange={handleChange} name='fname' />
<input type='text' placeholder='Enter Your LastName'
onChange={handleChange} name='lname' />
</div>
</form>
</>
)
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
The following will be the output if the user enters an input which is set with the help of handleChange() function as shown below:

Explanation: We have added name property in case of multiple inputs so that we can identify which input field fired the handleChange() function. We have used the spread operator because when the handleChange() function has been fired, we need the current value of fname and lname (because only then we can add(or assign) the entered input to the state variables(fname and lname)) which are stored in the state object, so we used spread operator to take an existing state object.
How this [name]: value statement works?
This statement assigns the value to that input field whose name (Property) matches the value.
Alternatives to the handleChange function
There are several alternatives to the handleChange function you provided here are a few of them :
Using an anonymous function:
HTML
<input type="text" onChange={(event) => setname(event.target.value)} />
Using a named function:
JavaScript
function handleInputChange(event) {
setname(event.target.value);
}
<input type="text" onChange={handleInputChange} />
Using the bind method:
HTML
<input type="text" onChange={setname.bind(null, event.target.value)} />
Similar Reads
How to use Fade Component in React JS ?
The Fade Component, available in Material UI for React, seamlessly adds a fade animation to a child element or component. Integrating this component into a React JS project is straightforward and enhances the user interface with ease.Prerequisites:React JSMaterial UIPopper Transitions: The Transitio
2 min read
How to create Functional Components in React ?
To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read
How to create a Functional Component in React?
To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read
State in React Functional Component
State in React functional components refers to a way of managing data that influences the rendering and behavior of components. Unlike class components, where state is managed using a this.state object, functional components use the React Hook useState to manage state efficiently.State is Local to t
3 min read
How to use Grow Component in ReactJS?
Grow Component in ReactJS adds a Grow animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Grow Component in ReactJS using the following approach.PrerequisitesReact JSMaterial UIApproachTo implement the
2 min read
How To Get Previous State In ReactJS Functional Component?
To get the previous state in ReactJS Functional Component we can use the previous value of the state while using the set state method. State can only be directly accessed in the class component so we will be using the react useState hook to accomplish the task.PrerequisitesReact JSReact JS functiona
2 min read
How to create components in ReactJS ?
Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
ReactJS Functional Components
In ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.What are Reactjs Functio
5 min read
Role of Hooks in Functional Components.
Hooks in React are like new tools that make building components easier. Before, we had to use classes, which were like following strict instructions. But with hooks, we can do the same thing more simply and flexibly directly inside functional components. It's like having a magic connector for buildi
3 min read
How to convert functional component to class component in ReactJS ?
ReactJS offers two primary ways of creating components: functional components and class components. While functional components are more concise and easier to reason about, there are situations where class components may be necessary with the flexibility to use lifecycle methods and manage the state
2 min read