nitin42 / react-fiber-resources

resources for React Fiber

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React Fiber resources PRs Welcome

This is for resources for React Fiber.

React Fiber is a new React reconciler algorithm.

Try React Fiber asynchronous rendering!

You can try React Fiber asynchronous rendering by the following ways.

1. Use ReactDOM.unstable_deferredUpdates

Inside a ReactDOM.unstable_deferredUpdates callback, the updates are treated as Low Priority.

ReactDOM.unstable_deferredUpdates(() => {
    ReactDOM.render(<App />. container);
    // or
    instance.setState(() => newState);
});

2. Use React.unstable_AsyncComponent

React.unstable_AsyncComponent makes updates in child components asynchronous, which means the updates are treated as Low Priority.

You can use React.unstable_AsyncComponent as just a component or as a base class like PureComponent.

const AsyncComponent = React.unstable_AsyncComponent;

<AsyncComponent>
  <App /> // Low Priority by default
</AsyncComponent>

// Low Priority by default
const App extends AsyncComponent {
    render() {
        return <Child />
    }
}

If you'd like to use synchronous updates inside the component, you can use ReactDOM.flushSync(cb). In side a ReactDOM.flushSync callback, the updates are treated as Sync Priority, which is the default priority of v15.

ReactDOM.flushSync(() => {
    // Sync Priority for use input or an animation etc
});

Examples

If you can't get any diferrence between Async mode and Sync mode, you should use CPU throttling on Chrome 😄

How to contribute React Fiber

React internal algorithm

If you are not familiar with React internals, I recommend you to read the documentations, which are very helpful.

React Fiber

Articles & Slides

Videos

React Fiber function call stacks

[Note] React Fiber now behaves as synchronous by default. See #8127. This call stacks are results in the time when it behaved as asynchronous.

ReactDOMFiber

React Fiber function call stack

ReactDOM

ReactDOM function call stack

ReactDOMFiber with 10000 items (Async Scheduling)

React Fiber function call stack with 10000 items (async)

--- working asynchronously using requestIdleCallback -------------------------------------------------
| ------- Fiber ---------------    ------- Fiber ---------------    ------ Fiber ---------------     |
| | beginWork -> completeWork | -> | beginWork -> completeWork | -> |beginWork -> completeWork | ... |
| -----------------------------   ------------------------------    ----------------------------     |
------------------------------------------------------------------------------------------------------
                      ↓↓↓
-----------------------------------------------------------------------
| commitAllWork(flush side effects computed in the above to the host) |
-----------------------------------------------------------------------

ReactDOMFiber with 10000 items (Sync Scheduling)

React Fiber function call stack with 10000 items (sync)

ReactDOM with 10000 items

ReactDOMFiber function call stack with 10000 items

React Fiber call tree

ReactDOMFiber call tree

Related Words

Custom Renderer Interface

You should implement the following interface when create a custom fiber renderer.

Toy custom renderers

ReactConsole.render(
  <>
    <red>Hello</red>
    <yellow>World</yellow>
    <cyan>React</cyan>
    <rainbow>Custom Renderer!</rainbow>
  </>,
  () => console.log(colors.inverse('##### Update ######'))
);
ReactConsole.render(
  <>
    <green>Hello</green>
    <yellow>World2</yellow>
    <cyan>React</cyan>
  </>
);
  • Voice Renderer
    ReactVoice.render(
      <>
        <alex>Hello</alex>
        <victoria>React Fiber</victoria>
      </>
    );

ReactNoop

ReactNoop is a renderer for React Fiber, which is using for testing and debugging. It is very useful to understand React Fiber renderer!! 👀

Bonus: You should watch ReactIncremental-test.internal.js, which helps to understand what React Fiber makes it possible

About

resources for React Fiber

License:MIT License


Languages

Language:JavaScript 100.0%