React Props
In React, Props are a mechanism for passing data from parent to child components. Props are read-only; child components cannot modify them directly, instead, they should be managed and updated by the parent component.
The main difference between state and props is that props is immutable, while state can change based on user interaction. This is why some container components need to define state to update and modify data. Child components can only pass data through props.
Using Props
Syntax for passing Props:
The following example demonstrates how to use props in a component:
React example
function
HelloMessage
(
props
)
{
return
<
h1
Hello
{
props.
name
}
!
h1
;
}
const
element
= <
HelloMessage
name
=
"
Runoob
"
/>;
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
element
)
;
Try it »
In the example, the name attribute is obtained through props.name.
Default Props
You can set default values for props through the defaultProps property of the component class, as shown below:
React example
class
HelloMessage
extends
React
.
Component
{
render
(
)
{
return
(
<
h1
Hello
,
{
this.
props
.
name
}
h1
)
;
}
}
HelloMessage
.
defaultProps
=
{
name
:
'
Runoob
'
}
;
const
element
= <
HelloMessage
/>;
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
element
)
;
Try it »
Multiple Props
You can pass multiple attributes to a child component.
Example
const
UserCard
=
(
props
)
=>
{
return
(
<
div
<
h2
{props.
name
}
h2
<
p
Age:
{
props.
age
}
p
<
p
Location:
{
props.
location
}
p
div
)
;
}
;
const
App
=
(
)
=>
{
return
(
<
UserCard name
=
"Alice"
age
=
{
25
}
location
=
"New York"
/>
)
;
}
;
ReactDOM.
render
(
<
App
/>,document.
getElementById
(
'root'
)
)
;
propTypes validation
propTypes is a tool provided by React for type-checking a component's props.
You can use the prop-types library to type-check a component's props.
In React, propTypes is defined as a static property of the component. First, you need to install the prop-types package:
npm install prop-types
Then, import prop-types in your component file and define the propTypes property. Here is an example
Example
import
PropTypes from
'prop-types'
;
const
Greeting
=
(
props
)
=>
{
return
<
h1
Hello
,
{props.
name
}
!
h1
;
}
;
Greeting.
propTypes
=
{
name
:
PropTypes.
string
.
isRequired
}
;
const
App
=
(
)
=>
{
return
(
<
div
<Greeting name
=
"Alice"
/>
{
/*
}
div
)
;
}
;
ReactDOM.
render
(
<
App
/>,document.
getElementById
(
'root'
)
)
;
Passing callback functions as Props
Functions can be passed as props to child components, which can call these functions to communicate with the parent component.
Example
class
ParentComponent
extends
React.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
message
:
''
}
;
}
handleMessage
=
(
msg
)
=>
{
this
.
setState
(
{
message
:
msg
}
)
;
}
;
render
(
)
{
return
(
<
div
<ChildComponent onMessage
=
{
this
.
handleMessage
}
/>
<
p
Message from Child
:
{
this
.
state
.
message
}
p
div
)
;
}
}
const
ChildComponent
=
(
props
)
=>
{
const
sendMessage
=
(
)
=>
{
props.
onMessage
(
'Hello from Child!'
)
;
}
;
return
(
<
div
<button onClick
=
{
sendMessage
}
Send Message
button
div
)
;
}
;
ReactDOM.
render
(
<
ParentComponent
/>,document.
getElementById
(
'root'
)
)
;
Destructuring Props
In function components, you can simplify code by destructuring props.
Example
const
Greeting
=
(
{
name
}
)
=>
{
return
<
h1
Hello
,
{
name
}!
h1
;
}
;
const
App
=
(
)
=>
{
return
<
Greeting name
=
"Alice"
/>;
}
;
ReactDOM.
render
(
<
App
/>,document.
getElementById
(
'root'
)
)
;
Summary
- Passing and using Props: the parent component passes data to the child component, which receives it through
propsprops. - Default Props: When using
defaultPropsSet default property values for the component. - PropTypes validation: When using
prop-typeslibrary to type-check props. - Passing callback functions: the parent component can pass functions as props to the child component to achieve communication between components.
- Destructuring Props: destructure props in function components to simplify code.
State and Props
The following example demonstrates how to use state and props together in an application. We can set state in the parent component and pass it to the child component via props. In the render function, we set name and site to get the data passed from the parent component.
React example
class
WebSite
extends
React
.
Component
{
constructor
(
)
{
super
(
)
;
this
.
state
=
{
name
:
"
Runoob Tutorial
"
,
site
:
"
https://www.runoob.com
"
}
}
render
(
)
{
return
(
<
div
<
Name
name
=
{
this.
state
.
name
}
/> <
Link
site
=
{
this
.
state
.
site
}
/>
div
)
;
}
}
class
Name
extends
React
.
Component
{
render
(
)
{
return
(
<
h1
{
this.
props
.
name
}
h1
)
;
}
}
class
Link
extends
React
.
Component
{
render
(
)
{
return
(
<
a
href
=
{
this.
props
.
site
}
{
this
.
props
.
site
}
a
)
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<
WebSite
/>
);
Try it »
Props validation
React.PropTypes was moved to after React v15.5 prop-types library.
Props validation uses propTypes, which can ensure our application components are used correctly. React.PropTypes provides many validators to verify whether incoming data is valid. When invalid data is passed to props, the JavaScript console will throw a warning.
The following example creates a Mytitle component whose title attribute is required and must be a string; non-string types will be automatically converted to strings:
React 16.4 example
var
title
=
"
Runoob Tutorial
"
;
//
var title = 123;
class
MyTitle
extends
React
.
Component
{
render
(
)
{
return
(
<
h1
Hello
,
{
this.
props
.
title
}
h1
)
;
}
}
MyTitle
.
propTypes
=
{
title
:
PropTypes
.
string
}
;
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<
MyTitle
title
=
{
title
}
/>
);
Try it »
React 15.4 example
var
title
=
"
Runoob Tutorial
"
;
//
var title = 123;
var
MyTitle
=
React
.
createClass
(
{
propTypes
:
{
title
:
React
.
PropTypes
.
string
.
isRequired
,
}
,
render
:
function
(
)
{
return
<
h1
{
this.
props
.
title
}
h1
;
}
}
)
;
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<
MyTitle
title
=
{
title
}
/>
);
Try it »
Of course, we can introduce more PropTypes validators and how to use them. Here are some commonly used PropTypes validators described below:
Basic Data Types
PropTypes.string: StringPropTypes.number: numberPropTypes.boolean: booleanPropTypes.object: objectPropTypes.array: arrayPropTypes.func: functionPropTypes.symbol:Symbol
Special types
PropTypes.node: anything that can be rendered: numbers, strings, elements, or arrays (including these types)PropTypes.element: React elementPropTypes.instanceOf(Class): an instance of a certain class
Combination types
PropTypes.oneOf(['option1', 'option2']): enum type, the value must be one of the provided optionsPropTypes.oneOfType([PropTypes.string, PropTypes.number]): one of multiple typesPropTypes.arrayOf(PropTypes.number): an array composed of a certain typePropTypes.objectOf(PropTypes.number): an object composed of a certain typePropTypes.shape({ key: PropTypes.string, value: PropTypes.number }): an object with a specific shape
Others
PropTypes.any: any typePropTypes.exact({ key: PropTypes.string }): an object with specific keys and no other extra keys
Here are some example codes showing how to use different PropTypes validators:
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom/client'
;
import
PropTypes from
'prop-types'
;
class
MyComponent
extends
React.
Component
{
static
propTypes
=
{
title
:
PropTypes.
string
.
isRequired
,
// must be a string and required
age
:
PropTypes.
number
,
// optional number
isAdmin
:
PropTypes.
bool
,
// optional boolean
user
:
PropTypes.
shape
(
{
// must be an object with a specific shape
name
:
PropTypes.
string
,
:
PropTypes.
string
}
)
,
items
:
PropTypes.
arrayOf
(
PropTypes.
string
)
,
// must be an array of strings
callback
:
PropTypes.
func
,
// optional function
children
:
PropTypes.
node
,
// optional renderable content
options
:
PropTypes.
oneOf
(
[
'option1'
,
'option2'
]
)
,
// must be one of specific values
}
;
render
(
)
{
return
(
<
div
<
h1
{
this.
props
.
title
}
h1
{
this
.
props
.
age
&&
<
p
Age:
{
this
.
props
.
age
}
p
}
{
this
.
props
.
isAdmin
&&
<
p
Admin
p
}
{
this.
props
.
user
&&
(
<
div
<
p
Name:
{
this
.
props
.
user
.
name
}
p
<
p
Email:
{
this
.
props
.
user
.
}
p
div
)
}
{
this
.
props
.
items
&&
(
<
ul
{
this.
props
.
items
.
map
(
(
item
,
index
)
=>
(
<
li key
=
{
index
}
{
item
}
li
)
)
}
ul
)
}
{
this
.
props
.
callback
&&
(
<
button onClick
=
{
this
.
props
.
callback
}
Click me
button
)
}
{
this
.
props
.
children
}
{
this
.
props
.
options
&&
<
p
Option:
{
this
.
props
.
options
}
p
}
div
)
;
}
}
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
"root"
)
)
;
root.
render
(
<
MyComponent
title
="Hello, World!"
age
=
{
30
}
isAdmin
=
{
true
}
user
=
{
{
name
:
"John Doe"
,
:
"john@example.com"
}
}
items
=
{
[
'Item 1'
,
'Item 2'
,
'Item 3'
]
}
callback
=
{
(
)
=>
alert
(
'Button clicked!'
)
}
options
=
"option1"
<
p
This is a child element
p
MyComponent
)
;