Different ways to access props inside a Component in React
Last Updated :
23 Jul, 2025
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
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.React.jsWhy Use React?Before React, web development faced issues like slow DOM updates
7 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsReact Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.Rendering List in ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.What are Reactjs Functio
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects