Before writing a React application, we need to set a goal. With a clear goal, we can complete our React development step by step in a more structured manner.
-
Write the
createElementfunction -
Write the
renderfunction -
Implement Concurrent Mode
-
Implement Fibers
-
Implement Render and Commit phases
-
Implement Reconciliation
-
Implement functional components
-
Implement basic Hooks
-
Set up a webpack compilation environment.
-
Test our React code using Jest.
-
Manage npm packages with Lerna.
import React from 'react';
function App() {
return <h1>Hello World</h1>;
}
---After transpilation with @babel/plugin-transform-react-jsx---
import React from 'react';
function App() {
return React.createElement('h1', null, 'Hello world');
}
Note: In React 17, JSX is no longer transpiled to call createElement. Instead, it automatically imports and invokes a new entry function from the React package.
-
Use
requestIdleCallbackfor polling to trigger task execution during idle time, and check for available work units. -
Process the object generated by
createElement, assign it as a work unit, wait for the browser's idle time to process the task, and mount it to the FiberRoot. -
Start processing the work unit with
performUnitOfWork. During the initial rendering, create the actual DOM node, but do not append it to the DOM tree yet (i.e., noappendChildcall at this stage). -
During work unit processing, execute
reconcileChildrento handle the reconciliation logic. Create new Fiber nodes, which include aflagsproperty to indicate UI mutations that need to be performed. -
When the traversal of the Fiber tree is complete, and each Fiber node has its mutation status determined, enter the Commit phase.
-
In the Commit phase: first, delete Fiber nodes marked with the
deletionflag. Then, start a depth-first traversal from thechildof the work-in-progress FiberRoot, and apply the necessary modifications to the actual DOM.
Install the Babel plugin for converting ESM to CommonJS:
npm install --save-dev @babel/plugin-transform-modules-commonjs
Add the following configuration to .babelrc:
{
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
}