Back to Course

Learn React

0% Complete
0/0 Steps
Lesson 5 of 32
In Progress

Props and State in React

Props and state are two important concepts in React that are used to store and manage data within a component. Understanding the difference between props and state is essential to building scalable and maintainable applications with the framework.

Props

Props, short for properties, are a way to pass data from a parent component to a child component. They are read-only and cannot be modified by the child component. Props are passed to the child component as an argument in the JSX element.

For example, consider the following Welcome component that takes in a name prop:

import React from 'react';

function Welcome(props) {
  return <h1>Welcome, {props.name}</h1>;
}

To use the Welcome component, you would pass in a name prop like this:

<Welcome name="John" />

The Welcome component would then render a heading with the value of the name prop:

<h1>Welcome, John</h1>

Props are typically used for static data that does not change within the lifetime of a component. They are a way to pass data down the component tree and are an important part of the unidirectional data flow in React.

State

State, on the other hand, is used to store and manage data that can change within the lifetime of a component. It is only available to class-based components and is created using the constructor() method.

For example, consider the following Welcome component that has a state with a name and loggedIn property:

import React from 'react';

class Welcome extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: props.name,
      loggedIn: false
    };
  }
  
  render() {
    return <h1>Welcome, {this.state.name}</h1>;
  }
}

The Welcome component has a constructor() method that sets the initial state based on the name prop that is passed to it. It also has a render() method that uses the name property of the state to render a greeting.

To update the state, you can use the setState() method, which is provided by the React.Component class. The setState() method takes an object that contains the properties that you want to update, and it merges the object with the current state.

For example, to update the loggedIn state to true, you would do the following:

this.setState({ loggedIn: true });

State is typically used for dynamic data that changes within the lifetime of a component. It is an important part of managing the data flow in a class-based component, and it should be used carefully to avoid unintended side effects.

Conclusion

Props and state are two important concepts in React that are used to store and manage data within a component. Props are used to pass data from a parent component to a child component, and they are read-only and cannot be modified by the child component. State is used to store and manage data that can change within the lifetime of a component, and it is only available to class-based components.

Props and state are both used to control the rendering and behavior of a component, but they should be used for different purposes. Props are typically used for static data that does not change within the lifetime of a component, while state is used for dynamic data that changes within the lifetime of a component.

It is important to understand the difference between props and state and to use them appropriately in your React components. By using props and state effectively, you can build scalable and maintainable applications with the framework.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

What are props in React?

Props, short for properties, are a way to pass data from a parent component to a child component. They are read-only and cannot be modified by the child component.

What are state in React?

State is used to store and manage data that can change within the lifetime of a component. It is only available to class-based components.

How do you pass props to a component?

To pass props to a component, you include them in the JSX element as attributes. For example: <Welcome name="John" />.

How do you create state in a class-based component?

To create state in a class-based component, you define a constructor() method that sets the initial state using the this.state property. For example: this.state = { name: 'John' };.

What is the setState() method and how do you use it?

The setState() method is a method provided by the React.Component class that is used to update the state of a class-based component. It takes an object that contains the properties that you want to update, and it merges the object with the current state. For example: this.setState({ name: 'John' });.