前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >读懂react源码

读懂react源码

作者头像
windseek
发布2019-11-07 19:42:21
4950
发布2019-11-07 19:42:21
举报
文章被收录于专栏:杨龙飞前端杨龙飞前端

2019-11-06

1、最重要的两个目录,react,react-dom,(react-reconciler后续会很重要)

2、使用flow检查js的类型

3、react.createElement(type, props, children)

4、createElement 源码在ReactElement.js里,最终会

return element = {

    // This tag allows us to uniquely identify this as a React Element

    $$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 creating this element.

    _owner: owner,

  };

5、createref,ref的三种形式,

    字符串形式:<input ref="inputref">

  回调用函数形式:<input ref={(input)=>{this.inputref=input}}>

  createRef形式:constrotor(){this.inputref = React.CreateRef();} <input ref = {this.inputref}>

 用法上,createRef,创建的ref取组件时多了一层current,例如this.inputref.current.value,回调和字符串形式的会直接使用this.inputref.value

6、React.forwardRef(props, ref=>{return <input />}) 可以创建一个带有ref的函数组件

7、通过context实现祖孙之间传递参数

parent.childContextTypes={

  value: PropTypes.string,

   a: PropTypes.string,

}

Child.contextTypes={

value: PropTypes.string,

  a: PropTypes.string,

}

最上层的组件必须提供provider   

const { Provider, Consumer } = React.createContext('default')

自组件可以通过this.context.。。。。。。来访问

如果在provider上,可以通过{value=>{<div>value</div>}};

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档