DEV Community

Cover image for Understanding State and Props in React ๐Ÿ’ป๐ŸŒฑ
Anik Dash Akash
Anik Dash Akash

Posted on

2 1 1

Understanding State and Props in React ๐Ÿ’ป๐ŸŒฑ

React is a powerful JavaScript library for building user interfaces. Two core concepts that every React developer should understand are state and props. In this blog, we'll break down these concepts, show how to use them, and illustrate them with code examples. ๐ŸŒฑ

What is State? ๐Ÿค”

In React, state is a built-in object that stores property values that belong to a component. It is managed within the component and can change over time. State allows React components to respond to user input and render dynamic data.

Example: Managing State in a Counter Component

Let's create a simple counter component that increments the count when a button is clicked.

import React, { useState } from 'react';

function Counter() {
  // Declare a state variable 'count' and a function 'setCount' to update the state
  const [count, setCount] = useState(0);

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

export default Counter;
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • useState(0) initializes the count state to 0.
  • setCount(count + 1) updates the count when the button is clicked.
  • React re-renders the component to display the updated count.

Output:

React State

When you click the button, the count increases by 1 each time.

What are Props? ๐Ÿ› ๏ธ

*Props *(short for properties) allow you to pass data from a parent component to a child component. Props are read-only and cannot be modified by the child componentโ€”theyโ€™re used for rendering dynamic data passed from one component to another.

Example: Passing Props to a Child Component

Letโ€™s create a component that takes a nameprop and displays a greeting message.

import React from 'react';

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

function App() {
  return (
    <div>
      <Greeting name="Anik" />
      <Greeting name="React Developer" />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The Greeting component accepts props as an argument.
  • {props.name} dynamically renders the name passed from the parent component.
  • The App component passes different names to the Greeting component.

Output:

react props

Differences Between State and Props ๐Ÿ“‹

Feature State Props
Mutability Can be changed with setState or useState Immutable (read-only)
Scope Local to the component (managed inside) Passed from parent to child
Usage Handles dynamic, interactive data Passes data and configurations

State and Props Together ๐Ÿ”—

Often, you'll use state in the parent component and pass its value down as props to the child component. Letโ€™s see how they work together.

Example: Updating State and Passing it as Props

import React, { useState } from 'react';

function DisplayCounter(props) {
  return <h1>Current Count: {props.count}</h1>;
}

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <DisplayCounter count={count} />
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

export default Counter;
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • count is managed as a state in the Counter component.
  • The DisplayCounter component receives count as a prop and displays it.
  • When the button is clicked, the count state is updated, and the new value is passed down to DisplayCounter as a prop.

Output:

state with props

Conclusion ๐Ÿ“

Understanding state and props is crucial for mastering React. State is used for managing data that changes over time, while props allow data to flow between components. By using these two concepts together, you can build interactive and dynamic user interfaces with ease.

Happy coding! ๐Ÿš€ Follow me on GitHub๐Ÿš€

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series ๐Ÿ“บ

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series ๐Ÿ‘€

Watch the Youtube series

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay