ReactJS: Props vs. State - Demystifying the Core Concepts

TanmayTanmay
3 min read

Published on: December 29, 2023

Tags: reactjs, javascript, web development

If you've delved into the world of ReactJS, you've likely encountered the common conundrum: "Props vs. State." These two concepts play a crucial role in how components manage and display data, but they can be a source of confusion, especially for newcomers. In this blog post, we'll dissect these concepts, providing a clear understanding of their differences and use cases.

Props: Passing the Baton of Data

What are Props?

Firstly, let's demystify the term. "Props" is simply a shorthand for "properties." At its core, props represent the data that a parent component passes down to its child components.

Props in Action

Consider the following example:

class Welcome extends React.Component {
  render() {
    return <h1>Hello {this.props.name}</h1>;
  }
}

const element = <Welcome name="Sara" />;

Here, the parent component (Welcome) passes a property (name) to its child component. Think of it as a function call where data is handed over to a component, making it dynamic and customizable.

The Flexibility of Props

Props are not only limited to external data injection; components can also define default props, making them optional:

class Welcome extends React.Component {
  render() {
    return <h1>Hello {this.props.name}</h1>;
  }
}

Welcome.defaultProps = {
  name: "world",
};

Now, if the Welcome component is invoked without a specified name, it defaults to "world."

Immutability of Props

One crucial point to note is that props should remain immutable throughout a component's lifecycle. Once set, they should not be changed, resembling a "pure" function. This immutability simplifies testing and ensures consistent behavior.

State: The Component's Private Journal

Introduction to State

While props are excellent for passing data from parent to child, "state" is the component's private stash of information. Unlike props, a component can manage and modify its state internally.

Stateless Beginnings

By default, a component starts as stateless, as shown in the Welcome component example earlier. It relies solely on props for its data.

State Unleashed: A Click Counter Example

Let's examine a simple component with state: a button that tracks the number of clicks.

class Button extends React.Component {
  constructor() {
    super();
    this.state = {
      count: 0,
    };
  }

  updateCount() {
    this.setState((prevState) => ({
      count: prevState.count + 1,
    }));
  }

  render() {
    return (
      <button onClick={() => this.updateCount()}>
        Clicked {this.state.count} times
      </button>
    );
  }
}

State Initialization: The Constructor

In this example, the Button component initializes its state in the constructor. The state, represented by { count: 0 }, signifies the starting point for our click counter.

The Magic of setState

The updateCount method showcases the power of setState. It takes a callback function, ensuring that state changes occur asynchronously. This mechanism allows access to the previous state (prevState), enabling seamless updates.

A Word of Caution: Proper Use of setState

While the temptation might arise to directly modify state attributes, like this.state.count = this.state.count + 1, resist it. Always use the setState method to trigger proper re-rendering and avoid potential pitfalls.

Rendering Dynamically with State

The button's text dynamically displays the current count from the component's state. The React magic lies in automatically re-rendering the component whenever the state changes.

Props and State: A Recap

In summary, understanding the distinction between props and state is pivotal for effective React development:

  • Props are the means of passing data from parent to child components. They are immutable and serve as a one-way street for external data.

  • State, on the other hand, is reserved for a component's internal data that can change over time. It allows components to manage and update their information dynamically.

By keeping these concepts separate and utilizing them appropriately, you'll unlock the full potential of ReactJS, building robust and maintainable components for your web applications. Happy coding!

10
Subscribe to my newsletter

Read articles from Tanmay directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tanmay
Tanmay

I am a self-taught software developer