在前端开发过程当中,源码解读是必不可少的一个环节,咱们直接进入主题,注意当前 React 版本号 16.8.6。javascript
注意:react 包文件仅仅是 React components 的必要的、功能性的定义,它必需要结合 React render一块儿使用(web下是 react-dom,原生app环境下是react-native)。即 react 仅仅是定义节点与表现行为的包,具体如何渲染、如何更新这是与平台相关的,都是放在react-dom、react-native 包里的。这是咱们只分析 web 环境的,即咱们不会只分析 react 包文件,会结合 react 包与 react-dom、react-reconciler 及其余相关包一块儿分析。前端
React 16.8.6 使用 FlowTypes 静态类型检查器,咱们须要在开发工具中支持 Fow(以 vscode 为例):java
安装 Flow Language Support
插件react
配置 workspace/.vscode/settings.json
git
{
"flow.useNPMPackagedFlow": true,
"javascript.validate.enable": false
}
复制代码
关于 Flow 更多请看 Flow官网。github
首先,从 react 入口,打开 react 源码库 index.js
:web
'use strict';
const React = require('./src/React');
// TODO: 决定顶层文件导出格式
// 虽然是旁门左道,但它可使 React 在 Rollup 和 Jest 上运行
module.exports = React.default || React;
复制代码
进入 ./src/React。json
其中 React 完整内容是:react-native
const React = { // React 暴露出来的 API
...
};
// Note: some APIs are added with feature flags.
// Make sure that stable builds for open source
// don't modify the React object to avoid deopts.
// Also let's not expose their names in stable builds.
if (enableStableConcurrentModeAPIs) {
React.ConcurrentMode = REACT_CONCURRENT_MODE_TYPE;
React.unstable_ConcurrentMode = undefined;
}
if (enableJSXTransformAPI) {
if (__DEV__) {
React.jsxDEV = jsxWithValidation;
React.jsx = jsxWithValidationDynamic;
React.jsxs = jsxWithValidationStatic;
} else {
React.jsx = jsx;
// we may want to special case jsxs internally to take advantage of static children.
// for now we can ship identical prod functions
React.jsxs = jsx;
}
}
export default React;
复制代码
其中,React 暴露出来的 API 有:api
Children: {
...
},
复制代码
提供处理 props.children 的方法,因为 props.children 是一个类数组的类型,能够用 React.Children 来处理
Component,
PureComponent,
createRef,
forwardRef,
复制代码
React.Component
相似,都是定义一个组件类。不一样是 React.Component
没有实现 shouldComponentUpdate()
,而 React.PureComponent
经过 props 和 state 的浅比较实现了。React.createRef()
createContext,
lazy,
memo,
error,
warn,
复制代码
createContext: context 建立方法
lazy: 实现异步加载的功能模块
memo: 也是一个高阶组件,相似于React.PureComponent
,不一样于React.memo
是 function 组件,React.PureComponent
是 class 组件。
useState,
useEffect,
useContext,
useCallback,
useImperativeHandle,
useDebugValue,
useLayoutEffect,
useMemo,
useReducer,
useRef,
复制代码
Hooks 是 React v16.7.0-alpha 开始加入的新特性,可让你在class之外使用state和其余React特性
其中 useState、useEffect、useContext 是 Hooks 三个最主要的API
Fragment: REACT_FRAGMENT_TYPE,
Profiler: REACT_PROFILER_TYPE,
StrictMode: REACT_STRICT_MODE_TYPE,
Suspense: REACT_SUSPENSE_TYPE,
复制代码
用 Symbol 来表示 React 的 Fragment、StrictMode、Suspense 组件
createElement: __DEV__ ? createElementWithValidation : createElement,
cloneElement: __DEV__ ? cloneElementWithValidation : cloneElement,
createFactory: __DEV__ ? createFactoryWithValidation : createFactory,
isValidElement: isValidElement,
复制代码
version: ReactVersion,
unstable_ConcurrentMode: REACT_CONCURRENT_MODE_TYPE,
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: ReactSharedInternals,
复制代码
这些就是 React 最主要的 API,下面 逐个击破,从应用到源码,一一吃透 React 。
附 V16 个个版本的更新内容:
React v16.0
React v16.1
React v16.2
React v16.3
React v16.4
React v16.5
React v16.6
React v16.7
React v16.8
React v16.9(~mid 2019)
想看更过系列文章,点击前往 github 博客主页
走在最后,欢迎关注:前端瓶子君,每日更新