Lädt...


🔧 🚀 Day 8: Preserving and Resetting State 🚀


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Welcome to Day 8 of my React Learning Journey! Today, I dived deeper into the state in React and how it is tied to a position in the render tree. This understanding is crucial for effectively managing component state and behavior in a React application. Here’s what I learned:

State and Render Tree

React builds render trees for the component structure in your UI. When you give a component state, the state doesn't live inside the component; instead, it is held inside React. React associates each piece of state it’s holding with the correct component by where that component sits in the render tree.

Example: Counter Component

In the example below, a Counter component is rendered in two different positions within the App component. Each instance of the Counter maintains its own state independently.

import { useState } from 'react';

export default function App() {
  const counter = <Counter />;
  return (
    <div>
      {counter}
      {counter}
    </div>
  );
}

function Counter() {
  const [score, setScore] = useState(0);
  const [hover, setHover] = useState(false);

  let className = 'counter';
  if (hover) {
    className += ' hover';
  }

  return (
    <div
      className={className}
      onPointerEnter={() => setHover(true)}
      onPointerLeave={() => setHover(false)}
    >
      <h1>{score}</h1>
      <button onClick={() => setScore(score + 1)}>
        Add one
      </button>
    </div>
  );
}

Each Counter component operates independently because they are rendered at different positions in the tree, even though they are the same JSX tag.

Isolated State

Each component on the screen has fully isolated state. For example, if you render two Counter components side by side, each will have its own independent state.

import { useState } from 'react';

export default function App() {
  return (
    <div>
      <Counter />
      <Counter />
    </div>
  );
}

function Counter() {
  const [score, setScore] = useState(0);
  const [hover, setHover] = useState(false);

  let className = 'counter';
  if (hover) {
    className += ' hover';
  }

  return (
    <div
      className={className}
      onPointerEnter={() => setHover(true)}
      onPointerLeave={() => setHover(false)}
    >
      <h1>{score}</h1>
      <button onClick={() => setScore(score + 1)}>
        Add one
      </button>
    </div>
  );
}

When one counter is updated, only the state for that specific component is updated:

State Persistence and Destruction

React will keep the state around for as long as you render the same component at the same position in the tree. If you remove a component, its state is destroyed. When the same component is rendered again, its state is initialized from scratch.

import { useState } from 'react';

export default function App() {
  const [showB, setShowB] = useState(true);
  return (
    <div>
      <Counter />
      {showB && <Counter />} 
      <label>
        <input
          type="checkbox"
          checked={showB}
          onChange={e => setShowB(e.target.checked)}
        />
        Render the second counter
      </label>
    </div>
  );
}

function Counter() {
  const [score, setScore] = useState(0);
  const [hover, setHover] = useState(false);

  let className = 'counter';
  if (hover) {
    className += ' hover';
  }

  return (
    <div
      className={className}
      onPointerEnter={() => setHover(true)}
      onPointerLeave={() => setHover(false)}
    >
      <h1>{score}</h1>
      <button onClick={() => setScore(score + 1)}>
        Add one
      </button>
    </div>
  );
}

When you stop rendering the second counter, its state disappears. When you render it again, it starts from its initial state.

Preserving State with Keys

To ensure a component maintains its state even when its position in the tree changes, you can use keys. Keys make React distinguish between different instances of the same component.

import { useState } from 'react';

export default function Scoreboard() {
  const [isPlayerA, setIsPlayerA] = useState(true);
  return (
    <div>
      {isPlayerA ? (
        <Counter key="Taylor" person="Taylor" />
      ) : (
        <Counter key="Sarah" person="Sarah" />
      )}
      <button onClick={() => setIsPlayerA(!isPlayerA)}>
        Next player!
      </button>
    </div>
  );
}

function Counter({ person }) {
  const [score, setScore] = useState(0);
  const [hover, setHover] = useState(false);

  let className = 'counter';
  if (hover) {
    className += ' hover';
  }

  return (
    <div
      className={className}
      onPointerEnter={() => setHover(true)}
      onPointerLeave={() => setHover(false)}
    >
      <h1>{person}'s score: {score}</h1>
      <button onClick={() => setScore(score + 1)}>
        Add one
      </button>
    </div>
  );
}

Here, switching between Taylor and Sarah does not preserve the state because they have different keys.

Conclusion

Today's learning session deepened my understanding of how React handles state in relation to the render tree. This knowledge is fundamental for managing component states efficiently and understanding the lifecycle of React components. As I continue my React learning journey, I look forward to applying these concepts to build more interactive and dynamic applications.

Stay tuned for more updates as I dive deeper into React!

Stay updated with my progress by following my LinkedIn and GitHub for detailed posts and code samples.

...

🔧 🚀 Day 8: Preserving and Resetting State 🚀


📈 53.22 Punkte
🔧 Programmierung

🔧 Day 4 of 90 Days of DevOps: Resetting the Jenkins Password on Windows and Setting Up CI with Jenkins


📈 27.11 Punkte
🔧 Programmierung

🔧 SPA-Like Navigation Preserving Web Component State


📈 26.11 Punkte
🔧 Programmierung

🔧 Using the booted Method in Laravel Eloquent Models for CRUD Event Listening and Cache Resetting


📈 22.54 Punkte
🔧 Programmierung

🔧 CSS Resetting And Normalizing


📈 22.54 Punkte
🔧 Programmierung

📰 Navigating the budgetary and infrastructure challenges of capturing, sharing and preserving data


📈 21.96 Punkte
📰 IT Security Nachrichten

📰 After Breaches At Other Services, Spotify Is Resetting Users' Passwords


📈 20.87 Punkte
📰 IT Security

📰 This Little App Prevents Windows 10 From Resetting Default Apps


📈 20.87 Punkte
📰 IT Security

📰 After Breaches At Other Services, Spotify Is Resetting Users' Passwords


📈 20.87 Punkte
📰 IT Security

📰 This Little App Prevents Windows 10 From Resetting Default Apps


📈 20.87 Punkte
📰 IT Security

📰 How To Stop Windows 10 App Defaults Resetting


📈 20.87 Punkte
🖥️ Betriebssysteme

📰 How To Stop Windows 10 App Defaults Resetting


📈 20.87 Punkte
🖥️ Betriebssysteme

🪟 How To Stop Windows 10 App Defaults Resetting


📈 20.87 Punkte
🪟 Windows Tipps

🪟 How To Stop Windows 10 App Defaults Resetting


📈 20.87 Punkte
🪟 Windows Tipps

📰 How To Fix Windows 10 Start Menu Resetting


📈 20.87 Punkte
🖥️ Betriebssysteme

🪟 How To Fix Windows 10 Start Menu Resetting


📈 20.87 Punkte
🪟 Windows Tipps

📰 How To Fix Let Apps Run In The Background Resetting On Windows 10


📈 20.87 Punkte
🖥️ Betriebssysteme

🪟 How To Fix Let Apps Run In The Background Resetting On Windows 10


📈 20.87 Punkte
🪟 Windows Tipps

🐧 having trouble resetting password


📈 20.87 Punkte
🐧 Linux Tipps

📰 Facebook Resetting Access Tokens for 90 Million Users After Breach


📈 20.87 Punkte
📰 IT Nachrichten

📰 Facebook Resetting Access Tokens for 90M Users After Breach


📈 20.87 Punkte
📰 IT Nachrichten

🕵️ Resetting Your GE Smart Light Bulb


📈 20.87 Punkte
🕵️ Reverse Engineering

📰 Slack is resetting passwords due to 2015 hack


📈 20.87 Punkte
📰 IT Security Nachrichten

📰 Slack is resetting passwords due to 2015 hack


📈 20.87 Punkte
📰 IT Security Nachrichten

📰 Slack Resetting More User Passwords in Response to 2015 Breach


📈 20.87 Punkte
📰 IT Security Nachrichten

📰 Slack Resetting More User Passwords in Response to 2015 Breach


📈 20.87 Punkte
📰 IT Security Nachrichten

🕵️ Slack resetting passwords for roughly 1% of its users


📈 20.87 Punkte
🕵️ Hacking

📰 Resetting Google Chrome to Default Settings


📈 20.87 Punkte
📰 IT Security Nachrichten

📰 Does resetting our emails password help prevent hacks?


📈 20.87 Punkte
📰 IT Security Nachrichten

📰 How to Reset PC on Windows 10 When Automatic Resetting Fails


📈 20.87 Punkte
🖥️ Betriebssysteme

🪟 How to Reset PC on Windows 10 When Automatic Resetting Fails


📈 20.87 Punkte
🪟 Windows Tipps

🐧 Bug 206813 - snd_hda_intel: No response from codec, resetting bus: [SOLVED]


📈 20.87 Punkte
🐧 Linux Tipps

📰 Need help factory resetting


📈 20.87 Punkte
📰 IT Security Nachrichten

🪟 Resetting Local Group Policy settings to defaults


📈 20.87 Punkte
🪟 Windows Tipps

🐧 [Grub] Windows 10 time keeps resetting to Greenwich time


📈 20.87 Punkte
🐧 Linux Tipps

matomo