Understanding React Props and State: A Beginner's Guide

React, a popular JavaScript library for building user interfaces, uses two main concepts to manage and pass data within components: Props and State. These concepts are pivotal in creating dynamic and interactive web applications. In this blog post, we'll dive deep into both Props and State, explaining their roles, differences, and how to use them effectively.

What are Props?

Props, short for properties, are a way of passing data from parent components to child components in React. They are read-only, which means they cannot be modified by the child components that receive them. Props are primarily used to render dynamic data (Dynamic data can include anything that might need to be displayed in the UI and could change based on user interaction or network responses—anything from text in a blog post to user profile details) and trigger state changes in other components.

Props are used to configure components in various ways when they are being created.

By using props to pass data, you can make your components reusable by giving different data inputs.

Props => Unidirectional Dataflow. i.e, Parent to Child.

How to Use Props

  1. Passing Props:
    function ParentComponent() {
        return <ChildComponent greet="Hello, World!" />;
    }
  2. Accessing Props in Child:
    function ChildComponent(props) {
        return <h1>{props.greet}</h1>;
    }
  3. Default Props: To ensure that your component has certain props even if they aren't passed by the parent, you can define default props:
    ChildComponent.defaultProps = {
        greet: "Hello, Default!"
    };

What is State?

State refers to a set of data that can change overtime, usually in response to user actions or events (like clicks, from inputs).

State is managed within the component (similar to variables declared within a function).

When the state of a component changes, the component re-renders to reflect the changes.

Characteristics

How to Use State

  1. Setting Up State: State is typically initialized in the constructor of a class component or using the useState hook in functional components.
    • Class Component:
      class ExampleComponent extends React.Component {
          constructor(props) {
              super(props);
              this.state = { count: 0 };
          }
      }
    • Functional Component:
      function ExampleComponent() {
          const [count, setCount] = useState(0);
      }
  2. Updating State: Never mutate the state directly. Use setState in class components or the updater function from useState in functional components.
    • Class Component:
      this.setState({ count: this.state.count + 1 });
    • Functional Component:
      setCount(count + 1);

Props vs. State

Feature Props State
Origin Received from parent Defined inside component
Mutability Immutable (read-only) Mutable (can change)
Use Pass data and functions Handle changes in component

Practical Drilling

  1. Prop Drilling: Sometimes, you need to pass props through multiple layers of components. This can be cumbersome and make your components tightly coupled. To solve this, consider using React context or state management libraries like Redux or MobX for deeper component trees.

  2. Component Reusability: Make components as independent as possible. Use props to pass data and avoid relying too much on the global state.

  3. State Updates Are Asynchronous: Always be aware that state updates might not apply immediately. For operations depending on the previous state, use the functional form of setState or the updater function.