props (short for "properties") are a mechanism for passing data and event handlers from one component to another, typically from a parent component to a child component.
Parent component
import React from 'react'; const App = () => { return ( <div> <Greeting name="Alice" /> <Greeting name="Bob" /> </div> ); }; export default App;
Child component that receives props
const Greeting = (props) => { return <h1>Hello, {props.name}!</h1>; };
Validating props ensures that a component receives the correct type of data, and the required props are provided. This helps to:
Prevent bugs.
Make the component more predictable
For example, if a component expects a string but receives a number, it could lead to unexpected behavior.
React provides a package called prop-types that allows you to enforce prop validation. If the props passed to the component do not match the expected types, React will log warnings in the console.
If you’re working in a new React project, you might need to install the prop-types package first:
npm install prop-types
You can define prop types by adding the propTypes object to your component.
import PropTypes from 'prop-types'; function Greeting(props) { return <h1>Hello, {props.name}!</h1>; } Greeting.propTypes = { name: PropTypes.string.isRequired, };
here: Adding PropTypes to validate the "name" prop
Expecting "name" to be a required string
Here are some common types of prop validations you can apply:
1. Primitive Types:
2. Required Props: Use .isRequired to enforce that a prop is passed:
Greeting.propTypes = { name: PropTypes.string.isRequired, };
3. Arrays of a Certain Type: You can validate arrays to ensure their contents are of a specific type:
MyComponent.propTypes = { items: PropTypes.arrayOf(PropTypes.string).isRequired, // Array of strings };
4. Objects of a Certain Shape:
MyComponent.propTypes = { user: PropTypes.shape({ name: PropTypes.string, age: PropTypes.number, }).isRequired, };
5. One of a Set of Values: You can enforce that a prop is one of several specified values:
MyComponent.propTypes = { status: PropTypes.oneOf(['success', 'error', 'loading']).isRequired, };
6. Custom Prop Validation: You can create your own custom validation logic:
MyComponent.propTypes = { age: function (props, propName, componentName) { if (props[propName] < 18) { return new Error( `${propName} in ${componentName} must be at least 18 years old` ); } }, };
Let’s create a component that expects several types of props and validate them:
import React from 'react'; const App = () => { return ( <div> <Greeting name="Alice" /> <Greeting name="Bob" /> </div> ); }; export default App;
You can also define default props using defaultProps in case a prop isn't provided.
const Greeting = (props) => { return <h1>Hello, {props.name}!</h1>; };
If a prop type is incorrect, React will log a warning in the browser console, but the application will still work. It’s important to note that PropTypes only run in development mode (i.e., they do not run in production builds).
The above is the detailed content of Props Validation in React. For more information, please follow other related articles on the PHP Chinese website!