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 Structure
App.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 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
3 min read
How to use componentWillMount() in React Hooks?
The componentWillMount() method allows us to execute the React code synchronously when the component gets loaded or mounted in the DOM (Document Object Model). This method is called during the mounting phase of the React Life-cycle. You cannot use any of the existing React lifecycle methods like Com
2 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 th
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 function
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
How to connect the components using Connect() in React Redux
In React Redux, you use the connect() function to connect your components to the Redux store. This function takes two parameters: mapStateToProps and mapDispatchToProps. mapStateToProps maps the store's state to the component's props, while mapDispatchToProps maps dispatch actions to props. By using
3 min read
How to conditionally render components in ReactJS ?
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. Itâs âVâ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. It is maintained by Facebook. PrerequisitesNodeJS or NPMReact JSReact
3 min read
How to implement useContext Hook in a Functional Component ?
In React, the useContext hook is used to consume values from a context. Context in React is a way to share values like themes, authentication status, or any other global state between components without explicitly passing the props through each level of the component tree. Syntax of useContext Hook:
2 min read