一篇文章带你理解ReactProps的原理.docx

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多

一篇文章带你理解ReactProps的原理

目录props理解1)props可以是:2)props在React充当角色(3个角度):3)监听props改变:操作props1、抽象props1)混入props2)抽离props2、注入props1)显式注入props2)隐式注入props总结

props理解

props是React组件通信最重要的手段

props:对于在React应用中写的子组件,父组件绑定在它们标签里的属性和方法,最终会变成props传递给它们。

1)props可以是:

①props作为一个子组件渲染数据源。②props作为一个通知父组件的回调函数。③props作为一个单纯的组件传递。④props作为渲染函数。⑤renderprops,和④的区别是放在了children属性上。⑥rendercomponent插槽组件。

/*children组件*/

functionChidrenComponent(){

returndivInthischapter,letslearnaboutreactprops!/div

/*props接受处理*/

classPropsComponentextendsReact.Component{

componentDidMount(){

console.log(this,_this)

render(){

const{children,mes,renderName,say,Component}=ps

constrenderFunction=children[0]

constrenderComponent=children[1]

/*对于子组件,不同的props是怎么被处理*/

returndiv

{renderFunction()}

{mes}

{renderName()}

{renderComponent}

Component/

button()=say()}changecontent/button

/div

/*props定义绑定*/

classIndexextendsReact.Component{

state={

mes:hello,React

node=null

say=()=this.setState({mes:letuslearnReact!})

render(){

returndiv

PropsComponent

mes={this.state.mes}//①props作为一个渲染数据源

say={this.say}//②props作为一个回调函数callback

Component={ChidrenComponent}//③props作为一个组件

renderName={()=divmynameisalien/div}//④props作为渲染函数

{()=divhello,world/div}{/*⑤renderprops*/}

ChidrenComponent/{/*⑥rendercomponent*/}

/PropsComponent

/div

2)props在React充当角色(3个角度):

①组件层级:

?父传子:props和子传父:props的callback将视图容器作为props进行渲染

②更新机制

?在fiber调和阶段中,diff可以说是React更新的驱动器,props可以作为组件是否更新的重要准则

?(PureComponent,memo等性能优化方案)

③插槽层面

?组件的闭合标签里的插槽,转化成chidren属性

3)监听props改变:

类组件:componentWillReceiveProps(废弃)componentWillReceiveProps(新)函数组件:useEffect(初始化会默认执行一次)propschidren模式

①props插槽组件

Container

Children

/Container

在Containe

文档评论(0)

184****8785 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档