react之——render prop
在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的更多相关文章
- [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 ...
- [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 ...
- React 之 render props 的理解
1.基本概念 在调用组件时,引入一个函数类型的 prop,这个 prop定义了组件的渲染方式. 2.回调渲染 回顾组件通信的几种方式 父-> 子 props 子-> 父 回调.消息通道 任 ...
- React中render Props模式
React组件复用 React组件复用的方式有两种: 1.render Props模式 2.高阶组件HOC 上面说的这两种方式并不是新的APi. 而是利用Raect自身的编码特点,演化而来的固定编码写 ...
- react解析: render的FiberRoot(三)
react解析: render的FiberRoot(三) 感谢 yck: 剖析 React 源码解析,本篇文章是在读完他的文章的基础上,将他的文章进行拆解和加工,加入我自己的一下理解和例子,便于大家理 ...
- React components render order All In One
React components render order All In One components render order / components lifecycle DOM tree ren ...
- REACT Missing “key” prop for element
https://stackoverflow.com/questions/48266018/missing-key-prop-for-element-reactjs-and-typescript Whe ...
- React在Render中使用bind可能导致的问题
因为bind在render的时候会重现生成,这样会导致props每次都不同, puremixin的插件也会失效. 所以需要将bind的结果缓存下来,或者直接在constructor里做这个事情 con ...
- [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 ...
随机推荐
- su 和sudo 命令
一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要添加用户任务,执行useradd ,beinan用 ...
- vue-cli脚手架搭建Vue.js项目
前提条件: 一.node.js 下载 https://nodejs.org/zh-cn/download/ 二.webpack 安装 npm install webpack -g PS:-g 就是 ...
- ERROR (UnicodeEncodeError): 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128
ERROR (UnicodeEncodeError): 'ascii' codec can't encode characters in position 0-1: ordinal not in ra ...
- linux中的调试知识---基础gdb和strace查看系统调用信息,top性能分析,ps进程查看,内存分析工具
1 调试一般分为两种,可以通过在程序中插入打印语句.有点能够显示程序的动态过程,比较容易的检查出源程序中的有关信息.缺点就是效率比较低了,而且需要输入大量无关的数据. 2 借助相关的调试工具. 3 有 ...
- 【Hadoop】MapReduce笔记(三):MapReduce的Shuffle和Sort阶段详解
一.MapReduce 总体架构 整体的Shuffle过程包含以下几个部分:Map端Shuffle.Sort阶段.Reduce端Shuffle.即是说:Shuffle 过程横跨 map 和 reduc ...
- python 中 模块,包, 与常用模块
一 模块 模块:就是一组功能的集合体, 我们的程序可以直接导入模块来复用模块里的功能 导入方式 一般为 : import 模块名 在python中, 模块一般分为四个通用类别 1使用python编写. ...
- E20180506-hm
更新: 2019/02/19 原来忘记分类,把此博文归入单词类 criterion n. 规范; (批评.判断等的) 标准,准则; criteria n. (批评.判断等的) 标准,准则( crit ...
- OpenCV第二课,嵌入MFC
单纯显示图片,只需链接 opencv_core2411d.libopencv_highgui2411d.lib 1.首先创建一个MFC的dlg,添加Picture Control控件 void CCO ...
- Unity 组件
组件(Component)这个概念最早是在2005年<Game Programming Gems 5>的<Component Based Object Management>中 ...
- C++内存泄漏检测
CRT检测 定位内存泄漏位置 #include "stdafx.h" #ifdef _DEBUG #define DEBUG_NEW new( _NORMAL_BLOCK, __F ...