No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home React

How to use React Hook: useState

by npn
December 15, 2020
in React
Reading Time: 4 mins read
0
How to use React Hook: useState
0
SHARES
433
VIEWS
Share on FacebookShare on Twitter

Contents

  • 1 Introduction
  • 2 Find out
    • 2.1 Check out React release
    • 2.2 Find the part where useState is exported
    • 2.3 Exploring the implementation earnestly
  • 3 Check it out
  • 4 At the end
Rate this post

Introduction

Of the hooks for achieving side effects in React’s Functional Component, I investigated how the most obvious useState is achieved.

Find out

Check out React release

Clone React from github and check out the latest v17.0.1 tag at the time of writing

$ git clone git@github.com:facebook/react.git
$ cd react
$ git checkout v17.0.1

Find the part where useState is exported

When I opened it in VSCode and useStatesearched frankly, I found that line 95 of React.js was exported, and its implementation was line 80 of ReactHooks.js.

Exploring the implementation earnestly

const dispatcher = ReactCurrentDispatcher.current

So ReactCurrentDispatcher.currentI need to find out what it is.
ReactCurrentDispatcherImplementation is ↓

const ReactCurrentDispatcher = {
  /**
   * @internal
   * @type {ReactComponent}
   */
  current: (null: null | Dispatcher),
};

export default ReactCurrentDispatcher;

Since this is all, ReactCurrentDispatcher.current =I searched for the fact that there must be a part where the value is pushed into the current, and I found that 112 cases were caught in 7 files, and I was about to break my heart quickly.

However, maybe the value is set from various places, so it would be nice if I could get a static Dispatcher, so if DispatcherI investigated what I did not see, useStatethe interface in it was there.

Then, if you check the implementation of Dispatcher, it seems that it will be enough, so check the implementation of Dispatcher.

I should have imported Dispatcher in the implementation part, so when type {DispatcherI searched for it, ReactPartialRendererHooks.jswas importing it, and it was useStateexporting the implementation of other hooks.
I did it.

Looking at the essential useState implementation, useReducer is used internally, so this time useReducerread the implementation.

useStateKara useReducerreducer, which is passed to the, basicStateReducerso the useStateteeth useReducerhow that is implemented as, special of.

ADVERTISEMENT

useReducerIn isReRenderhave the flag is referenced that, probably at the time of re-drawing wonder this flag is set.

If you read the implementation that is not redrawing, for the time being, it seems that the implementation of Dispatch is made in this, and when this is called, redrawing runs.
I don’t care about the redrawing mechanism because it is not the subject of this survey.

Next, when I read the implementation of redrawing, memoizedStateit seems that the value is cached in and the updated value and the already created Dispatch are returned.

That’s all Hooks itself is doing, and the rest is Dispatchthe part to run redrawing, so that’s it.

Check it out

At least useStatethe useReducerthey did not do is a big deal in the only state management.
The big thing is probably the part to redraw with Dispatch.
I would like to investigate that part one by one.

When I used it, I declared it first, so I felt like the reference I made once was being reused, but every time it was drawn, a new reference was returned. That’s right.
There is so many singleton state management that it seems difficult to prevent bugs, but I’m sure there is a library there.

At the end

Looking at the internal implementation, it is interesting to notice something new.
I wouldn’t have done it if I didn’t participate in the Advent calendar, so I’m glad I participated.

Cover photo from daveceddia.com

Tags: react hookuseState
ShareTweetShare
Previous Post

The interest of next/image from Next.js 10

Next Post

Deploy Next.js to Firebase for SSR and API Route

npn

Related Posts

React

Migrate from Hot Reload to Fast Refresh

December 16, 2020
1.5k
React

[React] React installation for Mac

December 16, 2020
320
React

Deploy Next.js to Firebase for SSR and API Route

December 15, 2020
1.7k
React

The interest of next/image from Next.js 10

December 15, 2020
596
Javascript

The story of migrating 70,000 lines of JavaScript code to TypeScript

December 15, 2020
508
Layout and Routing

AntD Basic layout – Create Website layout with Ant Design

September 23, 2020
5.1k
Next Post

Deploy Next.js to Firebase for SSR and API Route

Discussion about this post

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
Exit mobile version