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
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read