What are Props and State In React JS?

#state

#Props

In React, props and state are two important concepts used to manage data and render dynamic content in components. Understanding the differences and uses of props and state is crucial for building robust React applications.

Props (Properties)

Props are short for properties. They are read-only inputs passed from a parent component to a child component. Props allow you to pass data and event handlers to child components, enabling component reuse and dynamic content rendering.

Key Characteristics of Props:

  • Read-Only: Props are immutable, meaning a component cannot modify its own props.
  • Parent to Child: Data flows from the parent component to the child component.
  • Function Arguments: In functional components, props are received as function arguments. In-class components, they are accessed via this.props.

Usage Example:

  1. Functional Component with Props:
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

// Usage in Parent Component
function App() {
  return <Greeting name="John" />;
}

Class Component with Props:

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

// Usage in Parent Component
class App extends React.Component {
  render() {
    return <Greeting name="John" />;
  }
}

State

State is a built-in object in React that allows components to hold and manage their own data. Unlike props, the state is mutable and can be updated by the component itself. State is primarily used for managing data that changes over time or as a result of user interactions.

Key Characteristics of State:

  • Mutable: The state can be changed using the setState method in class components or the useState hook in functional components.
  • Internal to Component: The state is local to the component and cannot be accessed or modified by other components directly.
  • Triggers Re-Renders: When the state changes, the component re-renders to reflect the new state.

Usage Example:

  1. Class Component with State:
class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  increment = () => {
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.increment}>Increment</button>
      </div>
    );
  }
}

Functional Component with State:

import React, { useState } from 'react';

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

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

When to Use Props and State

  • Use Props when you want to pass data from a parent component to a child component and ensure that the data is not modified by the child component.
  • Use State when you need to manage data that is local to the component and can change over time, such as user inputs, form data, or dynamic content that responds to user actions.

Conclusion

Props and state are fundamental concepts in React that help manage data and render dynamic UIs. By understanding how and when to use props and state, you can build more modular, maintainable, and interactive React applications.

Further, if you have any questions, please visit our website, Gurulabs Website Design Agency.