本文是看視頻後進行的技術總結筆記,原視頻技術總結文檔直接點擊 React源碼解析文檔參考- https://react.jokcy.me/javascript
React 的調度過程 css
React 渲染更新的過程下面是 react暴露出來的api
React 頂層 API 官方文檔連接html
const React = {
Children: {
map,
forEach,
toarray,
only,
},
createRef,
Component,
PureComponent,
createContext,
forwardRef,
Fragment: REACT_FREGMENT_TYPE,
StrictMode: REACT_STRICT_TYPE,
unstable_AsyncMode: REACT_ASYNC_MODE_TYPE,
unstable_Profiler: REACT_PROFILER_TYPE,
createElemnt: _DEV_ ? createElmentWithValidation : createElment,
cloneElemnt: _DEV_ ? cloneElmentWithValidation : cloneElment,
createFactory: _DEV_ ? createFactoryWithValidation : createFactory,
isValidElemnt: isValidElemnt,
version: ReactVersion,
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: ReactSharedInternals,
}
複製代碼
ReactElement 經過 createElement建立 調用該方法須要傳入的三個參數:java
type是指 reactELement的類型react
export function createElement(type, config, children) {
// 處理參數
return ReactElement(
type,
key,
ref,
self,
source,
ReactCurrentOwner.current,
props
);
}
const ReactElement = function(type, key, ref, self, source, owner, props) {
const element = {
// This tag allows us to uniquely indentify this as React elemnt
$$typeof: REACT_ELEMENT_TYPE,
// Built-in properties that belong on the element
type: type,
key: key,
ref: ref,
props: props,
// Record the component responsible for crateing elment
_owner: owner
};
return element;
};
複製代碼
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class IgnoreFirstChild extends React.Component {
render() {
const children = this.props.children
return (
<div> <div>IgnoreFirstChild 組件</div> {React.Children.map(children, (child, i) => { // Ignore the first child if (i < 1) return return child })} </div>
)
}
}
ReactDOM.render(
<div> <IgnoreFirstChild > <div>first child</div> <div>second child</div> <div>third child</div> </IgnoreFirstChild > </div>, document.getElementById('container'), ); 複製代碼
效果圖: 動畫