React Forms and Events
In this section we will discuss how to use forms in React.
HTML form elements differ from other DOM elements in React because form elements inherently retain some internal state.
In HTML, elements like ,
A simple example
In the example, we set the value of the input field value = {this.state.data}. When the value of the input box changes, we can update the state. We can use onChange events to listen for changes to input, and modify state.
React Instance
class
HelloMessage
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
value
:
'
Hello Runoob!
'
}
;
this
.
handleChange
=
this
.
handleChange
.
bind
(
this
)
;
}
handleChange
(
event
)
{
this
.
setState
(
{
value
:
event
.
target
.
value
}
)
;
}
render
(
)
{
var
value
=
this
.
state
.
value
;
return
<div>
<input type="text" value={value}onChange={this>.
handleChange
}
/> <
h4
{
value
}
</
h4
</
div;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<
HelloMessage
/>
);
Try it »
The code above will render an input element with the value Hello Runoob!, and respond to the onChange event to update the value entered by the user.
Example 2
In the following example, we will demonstrate how to use forms on a child component. onChange The method will trigger an update of the state and pass the updated value to the child component's input box value then re-render the interface.
You need to create an event handler in the parent component (handleChange) , and as a prop (updateStateProp) passed down to your child components.
React Instance
class
Content
extends
React
.
Component
{
render
(
)
{
return
(
<div>
<input type="text" value={this>.
props
.
myDataProp
}
onChange
=
{
this
.
props
.
updateStateProp
}
/> <
h4
{
this
.
props
.
myDataProp
}
</
h4
</
div
);
}
}
class
HelloMessage
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
value
:
'
Hello Runoob!
'
}
;
this
.
handleChange
=
this
.
handleChange
.
bind
(
this
)
;
}
handleChange
(
event
)
{
this
.
setState
(
{
value
:
event
.
target
.
value
}
)
;
}
render
(
)
{
var
value
=
this
.
state
.
value
;
return
(
<
div
<
Content
myDataProp
=
{
value
}
updateStateProp
=
{
this.
handleChange
}
/>
div
)
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
'
root
'
)
)
;
root
.
render
(
<
HelloMessage
/>
);
Try it »
Select Dropdown Menu
In React, instead of using the selected attribute, use the value attribute on the root select tag to indicate the selected item.
React Instance
class
FlavorForm
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
value
:
'
coconut
'
}
;
this
.
handleChange
=
this
.
handleChange
.
bind
(
this
)
;
this
.
handleSubmit
=
this
.
handleSubmit
.
bind
(
this
)
;
}
handleChange
(
event
)
{
this
.
setState
(
{
value
:
event
.
target
.
value
}
)
;
}
handleSubmit
(
event
)
{
alert
(
'
Your favorite flavor is:
'
this
.
state
.
value
)
;
event
.
preventDefault
(
)
;
}
render
(
)
{
return
(
<form onSubmit={this>.
handleSubmit
}
<
label
Choose your favorite website <
select
value
=
{
this
.
state
.
value
}
onChange
=
{
this
.
handleChange
}
<option value="gg" Google> </option> <option value="rn" Runoob> </option> <option value="tb" Taobao> </option> <option value="fb" Facebook> </option> </select> </label> <input type="submit" value="Submit">/>
form
)
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
< FlavorForm /> );
Try it »
Multiple Forms
When you have multiple input elements to handle, you can add a name attribute to each element so that the handler chooses what to do based on the value of event.target.name.
React Instance
class
Reservation
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
isGoing
:
true
,
numberOfGuests
:
2
}
;
this
.
handleInputChange
=
this
.
handleInputChange
.
bind
(
this
)
;
}
handleInputChange
(
event
)
{
const
target
=
event
.
target
;
const
value
=
target
.
type
===
'
checkbox
'
?
target
.
checked
:
target
.
value
;
const
name
=
target
.
name
;
this
.
setState
(
{
[
name
]
:
value
}
)
;
}
render
(
)
{
return
(
< form < labelwhether to leave: <
input
name
=
"
isGoing
"
type
=
"
checkbox
"
checked
=
{
this
.
state
.
isGoing
}
onChange
=
{
this
.
handleInputChange
}
/>
label
<
br
/> <
label
Visitor count: <
input
name
=
"
numberOfGuests
"
type
=
"
number
"
value
=
{
this
.
state
.
numberOfGuests
}
onChange
=
{
this
.
handleInputChange
}
/>
label
</ form );
}
}
Try it »
React Events
The following example demonstrates how to modify data through the onClick event:
React Instance
class
HelloMessage
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
value
:
'
Hello Runoob!
'
}
;
this
.
handleChange
=
this
.
handleChange
.
bind
(
this
)
;
}
handleChange
(
event
)
{
this
.
setState
(
{
value
:
'
Runoob Tutorial
'
}
)
}
render
(
)
{
var
value
=
this
.
state
.
value
;
return
<div> <button onClick={this>.
handleChange
}
Click me
button
< h4 { value } </ h4 </ div;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
< HelloMessage /> );
Try it »
When you need to update the parent component's from a child component state , you need to create an event handler in the parent component (handleChange) , and as a prop (updateStateProp) passed down to your child components. Example below:
React Instance
class
Content
extends
React
.
Component
{
render
(
)
{
return
<div> <button onClick={this>.
props
.
updateStateProp
}
Click me
button
< h4 { this.
props
.
myDataProp
}
</ h4 </ div } } class HelloMessage extends React.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
value
:
'
Hello Runoob!
'
}
;
this
.
handleChange
=
this
.
handleChange
.
bind
(
this
)
;
}
handleChange
(
event
)
{
this
.
setState
(
{
value
:
'
Runoob Tutorial
'
}
)
}
render
(
)
{
var
value
=
this
.
state
.
value
;
return
< div < Content myDataProp = { value } updateStateProp = { this.
handleChange
}
Content
div
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
< HelloMessage /> );
Try it »