上一篇寫了Children,createRef,接着上一篇寫後面的幾個APIreact
const React = { Children: { map, forEach, count, toArray, only, }, createRef, Component, PureComponent, createContext, forwardRef, lazy, memo, useCallback, useContext, useEffect, useImperativeHandle, useDebugValue, useLayoutEffect, useMemo, useReducer, useRef, useState, Fragment: REACT_FRAGMENT_TYPE, Profiler: REACT_PROFILER_TYPE, StrictMode: REACT_STRICT_MODE_TYPE, Suspense: REACT_SUSPENSE_TYPE, createElement: createElement, cloneElement: cloneElement, createFactory: createFactory, isValidElement: isValidElement, version: ReactVersion, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: ReactSharedInternals, };
const emptyObject = {}; class Component { constructor(props, context, updater) { this.props = props; this.context = context; this.ref = emptyObject; this.updater = updater || null; } isReactComponent() {} setState(partialState, callback) { this.updater.enqueueSetState(this, partialState, callback, "setState"); } forceUpdate(callback) { this.updater.enqueueForceUpdate(this, callback, "forceUpdate"); } }
這裏我稍微改了一下源代碼,源代碼是用的function構造函數,這裏我覺的class看着會更簡單一些,實際上是一摸同樣的,Component的源代碼在ReactBaseClasses裏dom
並分賦值ide
補充一下源碼中可能用到了ReactNoopUpdateQueue,這個文件只是在dev中使用,僅僅給出一些提示而已函數
class PureComponent extends Component { constructor(props, context, updater) { super(props, context, updater); this.props = props; this.context = context; this.refs = emptyObject; this.updater = updater || null; } isPureReactComponent = true; }
isPureReactComponent = true
import { REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE } from "shared/ReactSymbols"; export function createContext(defaultValue, calculateChangedBits) { if (calculateChangedBits == undefined) { calculateChangedBits = null; } const context = { $$typeof: REACT_CONTEXT_TYPE, _calculateChangedBits: calculateChangedBits, _currentValue: defaultValue, _currentValue2: defaultValue, _threadCount: 0, Provider: null, Consumer: null }; (context.Provider = { $$typeof: REACT_PROVIDER_TYPE, _context: context }), (context.Consumer = context); return context; }
import { REACT_FORWARD_REF_TYPE } from "shared/ReactSymbols"; export default function forwardRef(render) { return { $$typeof: REACT_FORWARD_REF_TYPE, render }; }
可能不少人看了很疑惑,感受看完也沒什麼用,但這就是一個看react源碼的邏輯,不要一開始就過度追求從到到尾的實現,由於react代碼不少不少,我以爲一層一層的往裏看是比較好的方法,等看完這些,到了react-dom內容的時候,慢慢的就會發現這些API裏面的屬性它究竟是作什麼用的了。oop
累了明天再寫吧this