The Wayback Machine - https://web.archive.org/web/20240930162243/https://www.geeksforgeeks.org/reactjs-props-set-1/
Open In App

ReactJS Props – Set 1

Last Updated : 16 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Till now we were working with components using static data only. In this article, we will learn about how we can pass information to a Component.

What is props?

React allows us to pass information to a Component using something called props (which stands for properties). Props are objects which can be used inside a component.

Passing and Accessing props

We can pass props to any component as we declare attributes for any HTML tag. 

Syntax:

// Passing Props
<DemoComponent sampleProp = "HelloProp" />

In the above code snippet, we are passing a prop named sampleProp to the component named DemoComponent. This prop has the value “HelloProp”. Let us now see how can we access these props. 
We can access any props inside from the component’s class to which the props is passed.

Syntax:

// Accessing props
this.props.propName;

The ‘this.props’ is a kind of global object which stores all of a component’s props. The propName, that is the names of props are keys of this object.

Let us see an example where we will implement passing and accessing props.

In the below example, we will use a class-based component to illustrate the props. But we can also pass props to function-based components and going to pass in the below example.

To access a prop from a function we do not need to use the ‘this’ keyword anymore. Functional components accept props as parameters and can be accessed directly.

Open your react project and edit the index.js file in the src folder:

Example: Write the following code in your index.js file.

javascript




import React from 'react';
import ReactDOM from 'react-dom/client';
 
// sample component to illustrate props
class DemoComponent extends React.Component{
    render(){
        return(
                <div>
                    {/*accessing information from props */}
                    <h2>Hello {this.props.user}</h2>
                    <h3>Welcome to GeeksforGeeks</h3>
                </div>
            );
    }
}
 
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <DemoComponent />
  </React.StrictMode>
);


Output: This will be the output of the above program

gfg

Open your react project and edit the index.js file in the src folder:

Example: Write the following code in your index.js file.

Javascript




import React from 'react';
import ReactDOM from 'react-dom/client';
 
// functional component to illustrate props
function DemoComponent(props){
    return(
        <div>
            {/*accessing information from props */}
            <h2>Hello {props.user}</h2>
            <h3>Welcome to GeeksforGeeks</h3>
         </div>
    );
}
 
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <DemoComponent user="Geeks"/>
  </React.StrictMode>
);


Output:gfg

Passing information from one component to another

This is one of the coolest features of React. We can make components to interact among themselves. We will consider two components Parent and Children to explain this. We will pass some information as props from our Parent component to the Child component. We can pass as many props as we want to a component.
Look at the below code: 

Open your react project and edit the index.js file in the src folder:

Example: Write the following code in your index.js file.

javascript




import React from 'react';
import ReactDOM from 'react-dom/client';
 
// Parent Component
class Parent extends React.Component{
    render(){
        return(
                <div>
                    <h2>You are inside Parent Component</h2>
                    <Child name="User" userId = "5555"/>
                </div>
            );
    }
}
 
// Child Component
class Child extends React.Component{
    render(){
        return(
                <div>
                    <h2>Hello, {this.props.name}</h2>
                    <h3>You are inside Child Component</h3>
                    <h3>Your user id is: {this.props.userId}</h3>
                </div>
            );
    }
}
 
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <Parent/>
  </React.StrictMode>
);


Output: See the output of this program

gfg

What is this.props in React?

So we have seen props in React and also have learned about how props are used, how they can be passed to a component, how they are accessed inside a component, and much more. We have used the thing “this.props.propName” very often in this complete scenario to access props. Let us now check what is actually being stored in this. We will console.log “this.props” in the above program inside the child component and will try to observe what is logged into the console. Below is the modified program with console.log() statement: 

Open your react project and edit the index.js file in the src folder:

Example: Write the following code in your index.js file.

javascript




import React from 'react';
import ReactDOM from 'react-dom/client';
 
// Parent Component
class Parent extends React.Component{
    render(){
        return(
                <div>
                    <h2>You are inside Parent Component</h2>
                    <Child name="User" userId = "5555"/>
                </div>
            );
    }
}
 
// Child Component
class Child extends React.Component{
    render(){
        console.log(this.props);
        return(
                <div>
                    <h2>Hello, {this.props.name}</h2>
                    <h3>You are inside Child Component</h3>
                    <h3>Your user id is: {this.props.userId}</h3>
                </div>
            );
    }
}
 
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <Parent/>
  </React.StrictMode>
);


Output:gfg

You can clearly see in the above image that in the console it is shown that the this.props is an object and it contains all of the props passed to the child component. The props name of the child component are keys of this object and their values are values of these keys. So, it is clear now that whatever information is carried to a component using props is stored inside an object.

Note: Props are read-only. We are not allowed to modify the content of a prop. Whatever the type of Component is – functional or class-based, none of them is allowed to modify their props. We will learn about this in detail in the next article Reactjs | props set-2



Previous Article
Next Article

Similar Reads

How React handle or restrict Props to certain types, or require certain Props to exist ?
In React, data can be transmitted from a parent component to its children using props. PropTypes, a feature in React, facilitates runtime type checks on these props, issuing warnings in the browser console if the checks fail. This ensures data consistency and helps catch potential issues during development. Syntax: import PropTypes from 'prop-types
2 min read
How Do You Pass Props to `{this.props.children}`?
Passing props is important for sharing data and functionality between parent and child components, facilitating component reusability and maintaining a clear separation of concerns in React applications. In this article, we will explore two different approaches to Pass Props to {this.props.children}. Below are the possible approaches to Props to {t
4 min read
ReactJS Props - Set 2
In our previous article ReactJS Props - Set 1 we discussed props, passing and accessing props, passing props from one component to another, etc. In this article, we will continue our discussion on props. So, what if we want to pass some default information using props to our components? React allows us to do so. React provides us with something cal
4 min read
ReactJS Methods as Props
In this article, we will learn about props and passing methods as props. We will also discuss how we can use the child components to pass data to parent components using methods as props. What are props?We know that everything in ReactJS is a component and to pass in data to these components, props are used. Whenever we call child components from p
3 min read
ReactJS testInstance.props Property
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use testInstance.props property. The testInstance.props property is used to get a pr
1 min read
How to pass multiple props in a single event handler in ReactJS?
To pass multiple props in a single event handler in ReactJS we can create a function to handle the event that accepts multiple props or calls multiple methods. It is a normal task while creating a react project to perform multiple operations for some events. PrerequisitesReact JSReact JS PropsTo pass/call the multiple props methods in a single even
2 min read
How to pass props to ReactJS component that wrapped in variable ?
To pass props to a React JS component that is wrapped in a variable we have to create some copy of the component that can be modified. As we can't access the props of that component and pass a required argument we can clone it to make the required element and use it in our application. Props: Props means properties. Prop is an argument that we pass
2 min read
How to apply validation on Props in ReactJS ?
Need of Validating Props in React JS Props are used to pass the read-only attributes to React components. For the proper functioning of components and to avoid future bugs and glitches it is necessary that props are passed correctly. Hence, it is required to use props validation to improve the react component's performance. Prerequisites:NodeJS or
3 min read
What is the purpose of using super constructor with props argument in ReactJS ?
React, an open-source JavaScript library for UI design utilizes JSX to render components. Data is passed from parent to child components through props, which can only be updated by the parent. The `super()` function invokes the constructor of the parent class. Prerequisites:NodeJS or NPMReact JSReact PropsSteps to Create the React Application:Step
1 min read
ReactJS Props Reference
In React, components can receive information from a parent component by utilizing props (short for properties). A prop is an object accessible to all React components. It serves as a means to pass data from a parent component to a child component. <Welcome fullName = "Ashish Bhardwaj" />In the provided code, the prop `fullName` is passed with
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 between its opening and closing tags. A component w
5 min read
ReactJS State vs Props
In React props are a way to pass the data or properties from parent component to child components while the state is the real-time data available to use within only that component. States and props are two of the most important concepts of React and everything in React is based upon them. But before jumping into State and Props we need to know abou
4 min read
Destructuring of Props in ReactJS
Destructuring is a simple property that is used to make code much clear and readable, mainly when we pass props in React. Table of Content What is Destructuring?Advantages of DestructuringHow to use Destructuring?Using this.props methodUsing the Extraction methodUsing the Re-assigning methodSummaryWhat is Destructuring?Destructuring is a characteri
4 min read
React Suite Loader <Loader> Props
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. There is a lot of data that gets rendered on a web page. Sometimes it takes time to load up the data. This is when the Loader component allows the user to show the loading effect. It can be used to show the load
3 min read
React Suite <Pagination> Props
A React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite Prop <Pagination>. <Pagination> component is used to a
3 min read
React Suite Dropdown Extends button props
A React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite dropdown extended button props. As we know that the default value
4 min read
React Suite <Stack> Props
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite <Stack> props. <Stack> component is used to layout compo
3 min read
React Suite Grid <Col> Props
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. The grid component allows the user to provide 24 grids. It helps in achieving response design. There are different props for react suite grid rows and columns for the user to design. <Grid.Col> Props: as:
5 min read
React Suite <Sidenav> Props
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite Prop <Sidenav>. <Sidenav> component provides the collaps
3 min read
React.js Blueprint Component Tabs Props
React.js Blueprint is a front-end UI toolkit. It is very optimized and popular for building interfaces that are complex data-dense for desktop applications. React.js Blueprint Tabs Component allows the user to navigate between options in form of tabs. Blueprint Component Tabs Props: children: The Tab items serve as the children for this component.c
4 min read
How to access props inside quotes in React JSX?
Following approaches are there to access props inside quotes in React JSX: Approach 1: We can put any JS expression inside curly braces as the entire attribute value.Approach 2: We can use ES6 template literals.Creating React Application: Step 1: Create a React application using the following command: npx create-react-app foldernameStep 2: After cr
2 min read
What's the difference between super() and super(props) in React ?
Before going deep into the main difference, let us understand what is Super() and Props as shown below: Super(): It is used to call the constructor of its parent class. This is required when we need to access some variables of its parent class.Props: It is a special keyword that is used in react stands for properties. Used for passing data from one
3 min read
React Suite Button <ButtonGroup> Props
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. The Button component is used to fire an action when the user clicks the button. The ButtonGroup component is used to group the buttons together in a group. React Suite ButtonGroups Props: size: This property of
3 min read
React.js Render Props
The Render Props is a technique in ReactJS for sharing code between React components using a prop whose value is a function. Child component takes render props as a function and calls it instead of implementing its own render logic. In brief, we pass a function from the parent component to the child component as a render props, and the child compon
2 min read
React Suite Button ts:Color Props
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. The Button component is used to fire an action when the user clicks the button. ts:Color props are used to provide different colors to the Buttons. It provides different color options: 'red' | 'orange' | 'yellow
2 min read
React Suite Notification Props & Hooks
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. Notification Component allows the user to display a notification message globally. The notification is also used with a toaster in react-based applications. <Notification> Props: closable: It is a boolean
4 min read
What are props in React Native ?
Props are used to provide properties to the components using which they can be modified and customized. These properties are passed to the components when the component is created. Props are used in both user-defined and default components to extend their functionality. These props are immutable and cannot be changed after the creation of the compo
3 min read
What is the use of super(props) ?
Mostly developers just write super(props) as it makes no harm in code but no one is eager to know the reason how the thing is actually working and how it is useful. Let's dive more into the class components and find out the use of super(props) in the constructor. Here's a question arises, Why do we need super(props)? So, the simple answer to this q
3 min read
React Suite CheckTreePicker Props
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite CheckTreePicker Props. CheckTreePicker is used as multiple selectors
5 min read
React Suite Icon <Icon> Props
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. There is a lot of data that gets rendered on a web page. Sometimes we require to represent data using icons. This is when the Icon component allows the user to add icons to your application. Icons are commonly u
3 min read
Article Tags :