本文是看视频后进行的技术总结笔记,原视频技术总结文档直接点击 React源码解析文档参考- https://react.jokcy.me/javascript
React 的调度过程 css
下面是 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'), ); 复制代码
效果图: 动画