Skip to content

Latest commit

 

History

History
117 lines (86 loc) · 3.2 KB

useState.md

File metadata and controls

117 lines (86 loc) · 3.2 KB

Using State in React

State in React is a built-in object that allows components to create and manage their own local state. It is used to hold data that can change over time, typically in response to user actions or network responses. The ability to manage state is crucial for interactive applications, as it influences what is rendered on the screen.

Table of Contents

Introduction to State Updating State Using State with Functional Components Best Practices [Conclusion]{#Conclusion}


Introduction to State

In React, when the state of a component changes, the component re-renders to reflect the new state. This allows for dynamic user interfaces that can respond to user inputs or other events.

Tip

Key Points Each component can have its own state. Changes to the state trigger a re-render of the component. State is mutable, but should be updated immutably. Setting Up State

In class components, state is typically set up in the constructor method:

// Counter class component definition
class Counter extends React.Component {
  // Constructor to initialize state
  constructor(props) {
    super(props);
    this.state = { count: 0 }; // Initial state
  }
}

In functional components, the useState hook is used:

import React, { useState } from 'react';

// Functional Counter component definition
const Counter = () => {
  // State declaration using useState hook
  const [count, setCount] = useState(0); // Initial state
  return <h1>{count}</h1>;
};

Updating State

Class Components To update the state in a class component, you use the setState method:

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

Functional Components

In functional components, you call the state updater function returned by the useState hook:

// Function to increment the count
const increment = () => {
  setCount(count + 1);
};

Example of Updating State: Counter Component

Below is a complete example of a Counter component using state:

import React, { useState } from 'react';

// Functional Counter component definition
const Counter = () => {
  // State declaration using useState hook
  const [count, setCount] = useState(0);

  // Method to increment the count
  const increment = () => {
    setCount(count + 1);
  };

  // Render of component
  return (
    <div>
      <h1>{count}</h1>
      <button onClick={increment}>Increment</button>
    </div>
  );
};

Best Practices

  • Do Not Modify State Directly: Always use setState in class components or the updater function from useState in functional components to update state.

  • Use Functional Updates: When updating state based on the previous state, use a functional approach to ensure the update is applied correctly.

// Example of using functional updates
setCount(prevCount => prevCount + 1);
  • Lift State Up: If several components need to share the same state, consider lifting state up to their closest common ancestor.

  • Keep State Minimal: Store only the necessary state data. Calculated values can be derived from props or other state variables.

[EOF]