Introduction
In this chapter, we will explore React props, which are a fundamental concept in React for passing data from parent components to child components. We will understand what props are, how to use them, and their key features.
Table of Contents
- What are Props?
- Using Props in React
- Default Props
- PropTypes for Type Checking
- Key Features of Props
- Conclusion
What are Props?
Props, short for properties, are inputs to a React functional component. They are passed to the component via attributes in the JSX code. Props allow you to pass data from a parent functional component to a child functional component, enabling dynamic and reusable components.
Using Props in React
Props are read-only and cannot be modified by the child component. Let’s create an example to demonstrate how to use props in React.
Example:
- Create a New File for the Component: In the
srcdirectory, create a new file namedGreeting.js. - Define the Component: Add the following code to
Greeting.jsto create a functional component that uses props:// Greeting.js import React from 'react'; function Greeting(props) { return ( <div> <h1>Hello, {props.name}!</h1> </div> ); } export default Greeting; - Use the Component in
App.js: Opensrc/App.jsand modify theAppcomponent to use theGreetingcomponent:// App.js import React from 'react'; import Greeting from './Greeting'; function App() { return ( <div> <Greeting name="Amit" /> <Greeting name="Priya" /> <Greeting name="Ravi" /> </div> ); } export default App; - Save and View Changes: Save the files and go to your browser. You should see the greeting messages for “Amit,” “Priya,” and “Ravi.”
In this example, the Greeting component receives a name prop and uses it to display a personalized greeting message.
Default Props
You can define default values for props in case they are not provided by the parent component. This is done using the defaultProps property.
Example:
- Add Default Props: Modify the
Greetingcomponent to include default props:// Greeting.js import React from 'react'; function Greeting(props) { return ( <div> <h1>Hello, {props.name}!</h1> </div> ); } Greeting.defaultProps = { name: 'Guest' }; export default Greeting; - Use the Component in
App.js: You can omit thenameprop for one of theGreetingcomponents to see the default value in action:// App.js import React from 'react'; import Greeting from './Greeting'; function App() { return ( <div> <Greeting name="Amit" /> <Greeting name="Priya" /> <Greeting /> </div> ); } export default App; - Save and View Changes: Save the files and go to your browser. You should see the greeting messages for “Amit,” “Priya,” and “Guest.”
PropTypes for Type Checking
To ensure that components receive the correct type of props, you can use PropTypes for type checking. This helps in catching bugs and ensuring that components are used correctly.
Example:
- Install PropTypes: First, you need to install the
prop-typespackage:npm install prop-types - Add PropTypes to the Component: Modify the
Greetingcomponent to include PropTypes:// Greeting.js import React from 'react'; import PropTypes from 'prop-types'; function Greeting(props) { return ( <div> <h1>Hello, {props.name}!</h1> </div> ); } Greeting.defaultProps = { name: 'Guest' }; Greeting.propTypes = { name: PropTypes.string }; export default Greeting; - Use the Component in
App.js: Ensure thenameprop is passed correctly:// App.js import React from 'react'; import Greeting from './Greeting'; function App() { return ( <div> <Greeting name="Amit" /> <Greeting name="Priya" /> <Greeting /> </div> ); } export default App; - Save and View Changes: Save the files and go to your browser. You should see the greeting messages for “Amit,” “Priya,” and “Guest.” If a prop of the wrong type is passed, you will see a warning in the console.
Key Features of Props
- Data Passing: Props allow you to pass data from parent components to child components.
- Read-Only: Props are read-only and cannot be modified by the child component.
- Default Values: You can define default values for props using
defaultProps. - Type Checking: PropTypes provide a way to perform type checking on props, ensuring that components receive the correct type of data.
Conclusion
- Props is a special keyword in React that stands for properties. It is used to pass data from one component to another, mostly from the parent component to the child component.
- We can say props is a data carrier or a means to transport data.
- React props is an object that you get instantly when you create a React component.