useState Deep Dive

useState Deep Dive

Introduction

I have been using the useState hook in React and wanted to gain a deeper understanding of its inner workings. As a result, I wrote a blog post.

In this article, I will introduce a portion of my explanation, but for a more detailed understanding, please refer to my blog post:

useState in React

In React, the state of a component is updated using the useState hook in functional components. To understand how useState works, it's important to understand the concept of closures in JavaScript.

Closures allow a function to access and manipulate variables that are outside of its scope, including variables from its parent function.

In the case of useState, React uses closures to store the previous state value so that it can be updated when the component is re-rendered. This allows functional components to have state management capabilities similar to class components.


Rules of Hooks

No alt text provided for this image

According to the React documentation, Hooks must be called in the same order every time a component renders. The state values of a component are stored in an array that is keyed by the component. If Hooks are used inside a loop or conditional statement, the order of the saved state values may not match the original order, causing the incorrect state to be referenced and resulting in an error.

Summary

  • In React, to manage the state of a functional component,?we use values stored outside the component and access them through?closure?to compare and modify the state.
  • The?useState?hook modifies the value outside the component, so immediately after a state change,?the component still references the previous value.
  • Additionally, each component's state information is?stored in an array, so if a state-changing hook is used within a loop or conditional statement, it may reference the wrong value due to the hook being called in the wrong order.

要查看或添加评论,请登录

GeoJung Im的更多文章

  • Understanding Virtual DOM and React Fiber

    Understanding Virtual DOM and React Fiber

    Background of Virtual DOM Changes in DOM Color changes trigger painting (relatively fast). Changes in visibility or…

  • Layered Design - Functional Programming

    Layered Design - Functional Programming

    Layered Design in Software: Simplifying Code Complexity Layered design in software is a technique where software is…

  • Scope Chain & Call Stack

    Scope Chain & Call Stack

    Scope Chain The Scope Chain in JavaScript is a list-like structure that stores references to the global object and the…

  • Referencing Values with Refs

    Referencing Values with Refs

    This is the summarization of reading react docs. 1.

  • Getting into Functional Programming

    Getting into Functional Programming

    This is the summarization of reading a grokking functional programming book. Functional Programming Effects…

  • ?? Understanding TypeScript and JavaScript Relationship

    ?? Understanding TypeScript and JavaScript Relationship

    This is the summarization of reading Effective Typescript book. Summary TypeScript is a Superset of JavaScript: This…

  • Performance in Frontend

    Performance in Frontend

    What Does Performance Mean in Frontend Development? In frontend development, performance refers to how quickly and…

    2 条评论
  • Passing Data Deeply with Context

    Passing Data Deeply with Context

    This is what I summarized from react docs. Context lets the parent component make some information available to any…

  • Server Component and Client Component

    Server Component and Client Component

    Advantages of Server Components Data Fetching: It's fast to fetch data from the server because it is physically closer…

  • What is Closure

    What is Closure

    Let's get to know about closure. Closure is used to considered hard to understand.

社区洞察

其他会员也浏览了