Practice
When the application runs in development mode , React will automatically check all the props we set on components to make sure they have the correct type . If the type is wrong, React will generate warning messages in the console. It is disabled in production mode because of the performance impact. Required props are declared with
The set of predefined prop types:
We can define
import React from 'react'
import PropTypes from 'prop-types'
class User extends React.Component {
static propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number.isRequired
}
render() {
return (
<>
<h1>{`Welcome, ${this.props.name}`}</h1>
<h2>{`Age, ${this.props.age}`}</h2>
</>
)
}
}
Note: In React v15.5 PropTypes were moved out of
⬆ Back to top
Comments