在react “从上至下的数据流原则” 背景下,常规的消息传递机制就是通过prop属性,把父级数据传递给子级,这样一种数据流通模式决定了——数据的接收方子组件要被”硬植入“进数据的数据的给予方父组件,模式如下:

图1  传统依赖props进行数据传递的组件“硬植入”模型

基于上述的“硬植入”模式,就形成了组件之间的强耦合,进而会在代码里写出很多这种基于“功能型”中间件——大组件里嵌特定小组件。

那如果大组件里的数据要被多个子组件共享怎么办?如果继续“硬植入”要继续写出“功能型组件1”,“功能型组件2”。。。这种方式很显然破坏了前端功能化,让维护和扩展都变的艰难。

render prop

render prop的出现,让“子组件注入”变成了可能,在注入阶段进行父子组件之间的传值,从而实现了父子组件的解耦,具体参看react官网示例代码,本地demo了一下,贴上来:

class Cat extends React.Component{
render(){
const mouse = this.props.mouse;
return(
<div>
<img src={require("./cat.PNG")} style={{position:"absolute",left:mouse.x,top:mouse.y}} />
</div>
);
}
}
//带有函数prop的Mouse组件———render prop的由来
class Mouse extends React.Component{
constructor(){
super();
this.state = {
x:,
y:
};
this.handleMouseMove = this.handleMouseMove.bind( this );
}
handleMouseMove( e ){
this.setState( {
x: e.clientX,
y: e.clientY
} );
}
render(){//用render注入的方式,可以动态渲染注入对象
return (<div style={{height:"900px",position:"relative"}} onMouseMove={this.handleMouseMove}>
{this.props.render( this.state )}//调用render prop(函数引用),渲染注入的子组件
</div>);
}
}
class MouseTracker extends React.Component{
render(){
return (<div className="customized-wrapper">
<h1>Move the mouse around!</h1>
<Mouse
render={ mouse => (//render接收一个箭头函数引用,render作为一个prop传入子级
<Cat mouse={mouse}/>
) }
/>
</div>);
}
}

事实上,除了render prop之外,react组件也可以接收用户自定义的函数型属性作为注入依赖,当然也可以在父级调用该prop实现render或其他公用,用法于render类似,大家可在本地demo.

render prop实现了什么?

1. 为减少“硬植入”的功能型组件编写提供了解决方案,当然不能为了解耦而解耦,当某个父级只需要给一个子级传递数据时,大可不必进行这样的解耦操作

2. 提供了一种简单的“数据共享”机制,将数据使用方注入数据的提供方,实现了一种“按需供给”的机制,一份数据支持多方共享,很nice.

上述机制数据模型如下:

end

——May stars guide your way.

react之——render prop的更多相关文章

  1. [React] Integration test a React component that consumes a Render Prop

    In this lesson, I use Enzyme and Jest's Snapshot functionality to write an integration test for a co ...

  2. [React] Unit test a React Render Prop component

    In this lesson, I use Enzyme and Jest to unit test a Counter Render Prop component. Writing integrat ...

  3. React 之 render props 的理解

    1.基本概念 在调用组件时,引入一个函数类型的 prop,这个 prop定义了组件的渲染方式. 2.回调渲染 回顾组件通信的几种方式 父-> 子 props 子-> 父 回调.消息通道 任 ...

  4. React中render Props模式

    React组件复用 React组件复用的方式有两种: 1.render Props模式 2.高阶组件HOC 上面说的这两种方式并不是新的APi. 而是利用Raect自身的编码特点,演化而来的固定编码写 ...

  5. react解析: render的FiberRoot(三)

    react解析: render的FiberRoot(三) 感谢 yck: 剖析 React 源码解析,本篇文章是在读完他的文章的基础上,将他的文章进行拆解和加工,加入我自己的一下理解和例子,便于大家理 ...

  6. React components render order All In One

    React components render order All In One components render order / components lifecycle DOM tree ren ...

  7. REACT Missing “key” prop for element

    https://stackoverflow.com/questions/48266018/missing-key-prop-for-element-reactjs-and-typescript Whe ...

  8. React在Render中使用bind可能导致的问题

    因为bind在render的时候会重现生成,这样会导致props每次都不同, puremixin的插件也会失效. 所以需要将bind的结果缓存下来,或者直接在constructor里做这个事情 con ...

  9. [React Intl] Render Content Based on a Number using react-intl FormattedMessage (plural)

    Using the react-intl FormattedMessage component, we’ll learn how to render content conditionally in ...

随机推荐

  1. c++11 右值引用和移动语义

    什么是左值.右值 最常见的误解: 等号左边的就是左值,等号右边的就是右值 左值和右值都是针对表达式而言的, 左值是指表达式结束后依然存在的持久对象 右值是指表达式结束时就不再存在的临时对象区分: 能对 ...

  2. tyvj1391走廊泼水节——kruskal

    题目:http://www.joyoi.cn/problem/tyvj-1391 大意就是把一个树扩充成一个完全图,并且图中最小生成树仍是原来的树. 思路很巧妙,把边按权值从小到大排序,然后模拟加边的 ...

  3. 23.java方法的深入

    深入: public class MethodTest05{ public static void main(String[] args){ int i=m1(ture); System.out.pr ...

  4. Hadoop Shell 介绍

    以 hadoop 2.7.3 为例 bin 目录下是最基础的集群管理脚本, 用户可通过该脚本完成各种功能, 如 HDFS 管理, MapReduce 作业管理等. 作为入门, 先介绍bin 目录下的 ...

  5. SetConsoleCtrlHandler() -- 设置控制台信号处理函数

    http://www.groad.net/bbs/thread-8253-1-1.html 当你在命令行里工作时,经常会输入 Ctrl-C 这个组合键以停止程序的运行.SetConsoleCtrlHa ...

  6. ubuntu 安装 lamp 和配置虚拟机

    1:sudo passwd root  #设定root密码 su 切换  exit 退出  ,或者 普通用户下 加sudo  2:sudo apt-get update  #更新软件列表 3:sudo ...

  7. CodeForces Gym 100685J Just Another Disney Problem (STL,排序)

    题意:给定你大小未知的n个数,你允许有不超过一万次的询问,每次询问两个数,第i个数是否比第j个数小?然后后台会返回给你一个结果YES或者NO(即一行输入), 然后经过多次询问后,你需要给出一个正确的原 ...

  8. 【转】Win8 下 管理无线网络

    Ref:http://windows.microsoft.com/zh-CN/windows-8/manage-wireless-network-profiles 管理无线网络配置文件 适用于 Win ...

  9. STL-开篇

    基本概念 STL: Standard Template Library,标准模板库 定义: c++引入的一个标准类库 特点:1)数据结构和算法的 c++实现( 采用模板类和模板函数)2)数据的存储和算 ...

  10. mock api

    模客:http://mock-api.com/ easy-mock:https://www.easy-mock.com/ easy-mock动不动就挂了,而且用起来特别卡,不知道为什么那么多人推荐-_ ...