Certainly one of React’s core ideas is reusability via composable parts. Parts enable splitting advanced UI into separate, reusable items. Nevertheless, for parts to speak, they want a method to move information to one another. Enter props.
Props enable passing information from a dad or mum part to a toddler part. They’re like operate parameters, however for React parts.
Let’s take a look at a easy instance:
// Father or mother part
const Father or mother = () => {
return (
<Youngster
shade="blue"
onClick={handleClick}
/>
);
}
// Youngster part
const Youngster = (props) => {
return <div>{props.shade}</div>
}
The dad or mum part Father or mother
passes two props to the kid part Youngster
– a shade
string and an onClick
occasion handler.
The kid part receives these as a props
object and might entry them as props.shade
and props.onClick
.
Defining Props in a Part
To specify the props a part expects, you may outline them within the part operate or class:
// Perform part
const MyComponent = (props) => {
// ...
}
// Class part
class MyComponent extends React.Part {
// ...
}
React will verify that parts are handed all of the props they count on. This helps catch bugs early.
You can even set default values for props:
const MyComponent = (props) =>
Passing Props When Rendering Parts
When rendering a part, you move props like operate arguments:
// Father or mother part
<MyComponent
title={title}
content material={content material}
creator={creator}
/>
Entry these within the youngster part via props
.
Props are read-only within the youngster part. The kid can’t modify the props – this retains the information circulate unidirectional.
PropTypes for Validation
It’s a good suggestion to validate props being handed to a part. React offers a PropTypes module to specify prop varieties:
import PropTypes from 'prop-types';
const MyComponent = (props) => {
// ...
}
MyComponent.propTypes = {
title: PropTypes.string.isRequired,
content material: PropTypes.string.isRequired,
creator: PropTypes.form({
title: PropTypes.string.isRequired,
avatar: PropTypes.string
})
}
This validates props handed to MyComponent
. If invalid props are handed, a warning will seem within the console.
When to Use Props vs State
Whereas props enable passing information right into a part, state is used to trace modifications inside a part.
Use props for:
- Information that doesn’t change
- Initializing part state
- Passing information from dad or mum to youngster parts
Use state for:
- Information that modifications over time
- UI state primarily based on consumer interplay
- Re-rendering parts when information modifications
Getting the excellence proper takes apply – misusing props and state is a typical supply of bugs in React.
Conclusion
Props enable completely different parts to work collectively by passing information between them. Outline props a part expects, then move them when rendering:
// Father or mother
<Youngster title="Hi there" />
// Youngster
const Youngster = (props) => {
<h1>{props.title}</h1>
}
Props enable a unidirectional information circulate between mother and father and kids. Mixed with state to handle altering information, they make constructing reusable parts a lot simpler in React.