Deep Dive React 1 - 2

2022-08-16  本文已影响0人  吴摩西

Basic Concept

React Components

Form

const App = () => {
  return (
    <div>App Component</div>
  );
}

Return value

{
  "$$typeof": Symbol(react.element),
  key: null,
  props: { children: "App component" },
  ref: null,
  type: "div"
}
Screen Shot 2022-08-11 at 8.51.01 PM.png

React Element

Component Instance

Reconciliation

Rendering

React Fiber

it can

Two Reconciler phases

Rander Phase

Commit Phase

Fiber Properties

Fiber always has 1-1 relationship with something
* component instance, DOM node, etc.

export const FunctionComponent = 0;
export const ClassComponent = 1;
export const IndeterminateComponent = 2 // ?
export const HostRoot = 3; // the upmost root
export const HostComponent = 5; // an environment specified container, in web, like div, span
export const HostText = 6; // a text (maybe inner a span)
export const Fragment = 7; // a dom fragment <></>
export const Mode = 8; //?
export const ContextConsumer = 9; // a context consumer? rarely used
export const ContextProvider = 10; // context.Provider
export const ForwardRef = 11; // an element forwards ref?
export const Profiler = 12; // ?
export const SuspenseComponent = 13; // a Suspense ?
export const MemoComponent = 14; // component uses Rect.memo ?
export const SimpleMemoComponent = 15; // ?
export const LazyComponent = 16; // a component uses React.lazy ?
export const IncompleteClassComponent = 17; // ?
export const DehydratedFragment = 18; // a fragment used in dehydrate ?
export const SuspenseListComponent = 19; // ?
// no 20 ?
export const ScopeComponent = 21; // ?
export const OffscreenComponent = 22; // ?
export const LegacyHiddenComponent = 23; // ?
export const CacheComponent = 24; //?

fibers forms a tree

Screen Shot 2022-08-13 at 8.41.49 PM.png

Aren't Fiber really similar to react elements?

Partially true.

We could find createFiberFromElement, createFiberFromFragment, createFiberFromText

What's a work

Fiber Tree

There are actual two fiber trees, current tree is what displayed on screen, react won't change on that tree, instead, it change in workInProgress tree, after the changes are done, it will be swapped with current tree.
Gaming tech encounters similar questions that some in the screen isn't consistent with the others, they use a tech called double buffering, they wrote to a separate invisible buffer and when completed, they just swapped the buffers. React uses the same thing with the fiber trees during the commit phase.

Screen Shot 2022-08-15 at 8.28.17 AM.png

Swapping trees is not enough

What is an effect?

How are Effects applied

Fibers = units of work processed by functions like beginWork() and completeWork()

How react processes a fiber tree

tree processing

alternate

React Fiber Usase

Background Tasks API

https://developer.mozilla.org/en-US/docs/Web/API/Background_Tasks_API

The Cooperative Scheduling of Background Tasks API (also referred to as the Background Tasks API or the requestIdleCallback() API) provides the ability to queue tasks to be executed automatically by the user agent when it determines that there is free time to do so.

上一篇下一篇

猜你喜欢

热点阅读