Different ways to access props inside a Component in React
Last Updated :
19 Mar, 2024
The props keyword is the shorthand for properties. It is one of the most important features of React which helps in effective communication between various React components. It is a read-only attribute for passing data from the parent component to the child component. There are various ways to access props inside a component.
We will discuss the ways to access props inside a component.
Functional Components:
In this approach, props are passed as a parameter to a function. The props values can then be accessed within the function using the props keyword.
For example - props.propName - props is a keyword and propName is a property whose values are passed from the App.js component to the functional component.
Syntax:
const ComponentName = (props) => {
return <div>{props.propName}</div>
}
Example: Below is an example of accessing props using functional components.
JavaScript
import React from 'react';
const FunctionComponentName = (props) => {
return <div>{props.propName}</div>;
};
export default FunctionComponentName;
Output :
OUTPUT IMAGE FOR FUNCTIONAL COMPONENTS APPROACHClass Components:
In this approach props values will be accessed inside a class component using this keyword inside the JSX code written in render() method or any other class method.
For example - this.props.propName - where propName is the actual property whose values are being passed from App component to class component. and this.props is a way to access
Syntax:
class ComponentName extends React.Component {
render() {
return <div>{this.props.propName}</div>
}
}
Example: Below is an example of accessing props using class components.
JavaScript
import React from 'react';
class ClassComponentName extends React.Component {
render() {
return <div>{this.props.propName}</div>;
}
}
export default ClassComponentName;
Output:
OUTPUT IMAGE FOR CLASS COMPONENTS APPROACH
In this approach we extract the values of props (either objects or arrays) using method { propName }. This method can be used in both functional or class component.
Syntax:
const ComponentName = ({propName}) {
return <div>{propName}</div>
}
OR
class ComponentName extends React.Component {
const {propName} = this.props;
return <div>{propName}</div>
}
Example: Below is an example of accessing props using Destructuring.
JavaScript
import React from 'react';
const DestructureComponentName = ({ propName }) => {
return <div>{propName}</div>;
};
export default DestructureComponentName;
Output:
OUTPUT IMAGE FOR DESTRUCTURING
In this approach we start with creating context object and using ContextObject.Provider to provide the prop values from parent component to child component. ContextObject.Consumer is used to consume the prop values inside child component provided from parent component.
- createContext() - This is used for creating the Context object
- ContextProvider - This method will help to provide the context values
- ContextConsumer - This method is used to consume the context values
Syntax:
const ContextObject = React.createContext();
const ComponentName = () => {
<ContextObject.Consumer>
{
value => <div>{value}</div>
}
</ContextObject.Consumer>
}
Example: Below is an example of accessing props using context API.
JavaScript
// ContextObject.js
import React from 'react';
const ContextObject = React.createContext();
export default ContextObject;
JavaScript
// Parent.js
import React from 'react';
import Child from './Child';
import ContextObject from './ContextObject';
const Parent = () => {
return (
<ContextObject.Provider value="Geeks For Geeks">
<Child />
</ContextObject.Provider>
);
};
export default Parent;
JavaScript
// Child.js
import React from 'react';
import ContextObject from './ContextObject';
const Child = () => {
return (
<ContextObject.Consumer>
{
value => <div>{value}</div>
}
</ContextObject.Consumer>
);
};
export default Child;
Output:
OUTPUT IMAGE FOR CONTEXT API APPROACH
This approach is similar to context API approach. In this we start with creating context object and using ContextObject.Provider to provide the prop values from parent component to child component. To consume the prop values in child component we will be using useContext() hook.
- createContext(): This is used for creating the Context object
- ContextProvider: This method will help to provide the context values
- useContext() hook: This is used to consume the context values
Syntax:
const ContextObject = React.createContext();
const ComponentName = () => {
const value = useContext(ContextObject);
return <div>{value}</div>
}
Example: Below is an example of accessing props using useContext hook.
JavaScript
// ContextObject.js
import React from 'react';
const ContextObject = React.createContext();
export default ContextObject;
JavaScript
// Parent.js
import React from 'react';
import Child from './Child';
import ContextObject from './ContextObject';
const Parent = () => {
return (
<ContextObject.Provider value="Geeks For Geeks">
<Child />
</ContextObject.Provider>
);
};
export default Parent;
JavaScript
// Child.js
import React, { useContext } from 'react';
import ContextObject from './ContextObject';
const Child = () => {
const value = useContext(ContextObject);
return <div>{value}</div>;
};
export default Child;
Output:
OUTPUT IMAGE FOR useContext() HOOK APPROACHExample: Below is an example of accessing props inside a component.
JavaScript
import React from 'react';
import FunctionComponentName from './FunctionComponentName';
import ClassComponentName from './ClassComponentName';
import DestructureComponentName from './DestructureComponentName';
import Parent from './Parent';
function App() {
return (
<div>
<FunctionComponentName propName="Geeks For Geeks" />
<ClassComponentName propName="Geeks For Geeks" />
<DestructureComponentName propName="Geeks For Geeks" />
<Parent />
</div>
);
}
export default App;
Output:
OUTPUT IMAGE FOR USING ALL THE COMPONENTS FOR DIFFERENT APPROACH IN App.js FILE
Similar Reads
How to access props inside a functional component?
React is an open-source JavaScript library that is mainly used for developing User Interface or UI components. It is a single-page application that is popularly used for developing dynamic web interfaces. While building a React application, the React components serve as basic building blocks. In Rea
3 min read
How to use PropTypes in a Functional Component in React?
PropTypes is a mechanism provided by React for type-checking props passed to components. It allows developers to specify the expected types of props (such as strings, numbers, arrays, objects, etc.) and whether they are required or optional. PropTypes helps catch bugs early by providing warnings in
2 min read
How to access props.children in a stateless functional component in ReactJS ?
The {props. children} allows you to create a generic template, which can be modified by a parent when invoked. In this way, a parent component can easily pass whatever is necessary to its child, even generated layout features. Anytime you call a component, it automatically displays whatever it has b
5 min read
How are React Hooks different from class components in ReactJS?
React Hooks are helpful tools that make building websites and apps with React easier. They simplify how users handle things like data and app behavior, making the code cleaner and easier to understand. Class components in React are like the old-school way of building parts of your website or app. Th
2 min read
How to add Event Listeners to wrapped Component in ReactJS ?
React provides a powerful way of DOM manipulation by introducing the concept of virtual DOM. It is mainly used to build single-page applications. There are times when you need to add event listeners. This can be achieved by using the concept of forwarding refs and leveraging React's lifecycle method
3 min read
How to open a component in a new tab in React JS ?
React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL. In this tutorial, you will understand how to open a new component in another tab while
2 min read
What does it mean for a component to be mounted in ReactJS ?
For a component to be mounted in ReactJS it means to be initialized and inserted in the DOM. Mounting is the initial phase in which the instance of the component is created and inserted into the DOM. When the component gets successfully inserted into the DOM, the component is said to be mounted. In
2 min read
How to Access Props Inside Quotes in React JSX?
Props are used to pass data from parent to child components in react. We can access the props inside quotes in JSX like HTML attribute with the help of ES6 Template Literals and javascript expressions with curly braces ApproachTo access Props inside quotes in React JS we will use the curly braces {}
2 min read
When to use a Class Component over a Function Component in ReactJS?
ReactJS provides two main ways to create components: Class Components and Function Components. With the introduction of React Hooks in version 16.8, Function Components became more powerful and are now commonly preferred. However, there are still scenarios where Class Components might be a better ch
3 min read
Difference between Link and Navigate Component in React Router
In this article, we'll delve into the intricacies of two fundamental components within the widely acclaimed React Router Dom Library a Link and Navigate. As the backbone of many react applications, these components play pivotal roles in facilitating seamless navigation and routing. We'll explore the
4 min read