×

React Props

React Props

A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute value of a tag, and its work is similar to the HTML elements. It helps to pass data from one component to another components. Props are like function arguments, as props are passed to the component in a similar way as arguments passed in the function.

We cannot modify the props inside the component. We can add attributes called props inside the components. These attributes are available in the component as this.props and use for rendering the dynamic data in our render method.

When you require immutable data in the component, you need to add the reactDom.render() method in the  main.js file of the ReactJS project and use that file inside the component in which you need it.

For example:

App.js

import React, { Component } from 'react';

class App extends Component {
  render() {
    return (
      <div>
        <h2>Hello {this.props.name}</h2>
        <p>This is tutorialandexample.com website which provides you a lot of tutorials.</p>
      </div>
    );
  }
}

export default App;

Main.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('app'));

Output:

Hello world React props

Default Props

It is not mandatory to always add props in the reactDom.render() element. You can also set default props directly on the component constructor. It is clearly explained in the following example:

Example:

import React, { Component } from 'react';

class App extends Component {
  render() {
    return (
      <div>
        <h2>Hello World</h2>
        <h2>Default Props Example</h2>
        <p>Welcome to {this.props.name}</p>
        <p>tutorialandexample.com provides you one of the best tutorials.</p>
      </div>
    );
  }
}

App.defaultProps = {
  name: "tutorialandexample.com"
};

export default App;

Main.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('app'));

Output:

Default props

State and Props

State and props both can be combined in the app. You can set the state in the parent component and pass it to the child component using props.

For example:

App.js

import React, { Component } from 'react';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: "tutorialandexample.com",
    };
  }

  render() {
    return (
      <div>
        <Tutorial tutorialProp={this.state.name} />
      </div>
    );
  }
}

class Tutorial extends Component {
  render() {
    return (
      <div>
        <h2>State & Props Example</h2>
        <p>Welcome to {this.props.tutorialProp}</p>
        <p>tutorialandexample.com provides you one of the best tutorials.</p>
      </div>
    );
  }
}

export default App;

Main.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('app'));

Output:

States and Props

Related Topics

Comparison between React and Vue

Comparison between React and Vue React, and Vue both are the two most famous libraries of JavaScript that are used for creating thousands of websites today. The React and Vue both...

3 minutes read.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

React Fragments

React Fragments The fragments in React are introduced from the 16.2 and above versions. Fragments allow you to group a list of child elements without adding any extra node in the DOM. In React,...

2 minutes read.

React Components

React Components Former, the developers used to write more than thousands of lines of code to develop a single-page application. These applications follow the structure of traditional DOM, and it was very challenging to make...

2 minutes read.

React Lists

React Lists Lists are used for displaying the data in an ordered format and mainly used to display the menus on websites. The creating of lists in React is as similar...

2 minutes read.

React Higher-Order Components

React Higher-Order Components In short form, Higher-Order Components are represented as HOC. The Higher-order component is an advanced technique to use component logic. HOC is a function that takes the component and returns the new...

2 minutes read.

React Events

React Events An event is an action that triggers a response of the user action or system-generated event. As HTML, React can also perform actions based on user events. React has the...

2 minutes read.

React Props Validation

React Props Validation As we know, props are a suitable mechanism for passing the read-only attributes to react components. The props are required to be correctly used in the component. If it is...

5 minutes read.

React State

React State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over time. The change...

3 minutes read.

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 minutes read.

React Component API

React Component API It is a top-level API. It provides reusability to the code in the application and makes it completely individual. It has several methods for: Creating Elements.Transforming Elements.Fragments. Now, we are explaining the three...

2 minutes read.

React Native vs ReactJS

Difference between React Native and ReactJS React Native React Native is also an open-source JavaScript framework that is used for the development of a mobile application for iOS Android and Windows. It...

6 minutes read.

React Animation

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

4 minutes read.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

3 minutes read.

React Component Life-Cycle

React Component Life-Cycle In ReactJS, the creation process of every component includes several lifecycle methods. These methods, together stated as component's lifecycle. Four phases of the component's life cycle are as...

4 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

3 minutes read.

Difference between Controlled and Uncontrolled component in ReactJs

Controlled Component In the controlled component, the input of the form element is handled by the component rather than the DOM. Controlled components have the functions that govern the data, which...

2 minutes read.

React Flux

React Flux Introduction It is an application architecture that is internally used by Facebook to build the client-web application with react. It is useful when the project includes dynamic data, and...

2 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

ReactJS Versions

ReactJS Versions The Complete set of release history of ReactJS is elaborated as follows. The complete set of full documentation of recent releases is on GitHub. S.noVersionRelease DateExplanation1.0.3.029/05/2013Initially released for public.2.0.4.029/07/2013Supporting comments...

4 minutes read.