More detail check LInk.

Render Prop vs HOC:

HOC version for withMouse:

import React from 'react'
import ReactDOM from 'react-dom' const withMouse = (Component) => {
return class extends React.Component {
state = { x: 0, y: 0 } handleMouseMove = (event) => {
this.setState({
x: event.clientX,
y: event.clientY
})
} render() {
return (
<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
<Component {...this.props} mouse={this.state}/>
</div>
)
}
}
} const App = React.createClass({
render() {
// Instead of maintaining our own state,
// we get the mouse position as a prop!
const { x, y } = this.props.mouse return (
<div style={{ height: '100%' }}>
<h1>The mouse position is ({x}, {y})</h1>
</div>
)
}
}) // Just wrap your component in withMouse and
// it'll get the mouse prop!
const AppWithMouse = withMouse(App) ReactDOM.render(<AppWithMouse/>, document.getElementById('app'))

Problems:

  • Indirection. We still have the same problem with indirection that we had when we were using mixins. Except this time instead of wondering where our state comes from we’re wondering which HOC provides which props.
  • Naming collisions. Unfortunately we still have this problem too. Two HOCs that try to use the same prop name will collide and overwrite one another, except this time it’s slightly more insidious because React won’t warn us about the prop name collision.

Render Prop:

import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types' // Instead of using a HOC, we can share code using a
// regular component with a render prop!
class Mouse extends React.Component {
static propTypes = {
render: PropTypes.func.isRequired
} state = { x: 0, y: 0 } handleMouseMove = (event) => {
this.setState({
x: event.clientX,
y: event.clientY
})
} render() {
return (
<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
{this.props.render(this.state)}
</div>
)
}
} const App = React.createClass({
render() {
return (
<div style={{ height: '100%' }}>
<Mouse render={({ x, y }) => (
// The render prop gives us the state we need
// to render whatever we want here.
<h1>The mouse position is ({x}, {y})</h1>
)}/>
</div>
)
}
}) ReactDOM.render(<App/>, document.getElementById('app'))
  • Indirection. We don’t have to wonder where our state or props are coming from. We can see them in the render prop’s argument list.
  • Naming collisions. There is no automatic merging of property names, so there is no chance for a naming collision.

Render Prop give some kind of feelings that, in the parent component, you pass a function into Child component's prop. This function is defining how the Child component should look like. The Child component just need call the function and pass in the state which needed for rendering.

[React] Use a Render Porp的更多相关文章

  1. react 实现pure render的时候,bind(this)隐患

    react 实现pure render的时候,bind(this)隐患 export default class Parent extends Component { ... render() { c ...

  2. 使用React.Fragment替代render函数中div的包裹

    1.在 React 中,render 函数中 return 的内容只能有一个根节点,如果多个元素嵌套,需要用一个标签元素包裹 这个包裹的标签通常用 div,示例如下: class App extend ...

  3. react props与render成员函数

    props是组件固有的属性集合,其数据由外部传入,一般在整个组件的生命周期中都是只读的,React的API顶层设计也决定了这一点.属性初值通常由React.createElement函数或者JSX中标 ...

  4. react初学之render返回加括号的问题

    刚在学习react的初始阶段,跑了一段代码 var  Mydom = React.createClass({ render:function(){ return <div> <inp ...

  5. React 的 server render 初步学习

    所谓server render 即服务端渲染,这是为了解决现代前端框架下的单页应用在SEO方面不友好的问题. react 的SSR主要思路就是 1.将应用的根组件导出 如 <App /> ...

  6. [React Router v4] Render Multiple Components for the Same Route

    React Router v4 allows us to render Routes as components wherever we like in our components. This ca ...

  7. [React Router v4] Render Catch-All Routes with the Switch Component

    There are many cases where we will need a catch-all route in our web applications. This can include ...

  8. [React Router v4] Render Nested Routes

    With React Router v4 the entire library is built as a series of React components. That means that cr ...

  9. React js ReactDOM.render 语句后面不能加分号

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

随机推荐

  1. CF 986A Fair(多源BFS)

    题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...

  2. 【Codeforces Round #422 (Div. 2) B】Crossword solving

    [题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...

  3. 【Codeforces Round #421 (Div. 2) A】Mister B and Book Reading

    [题目链接]:http://codeforces.com/contest/820/problem/A [题意] 每天看书能看v页; 且这个v每天能增加a; 但是v有上限v1; 然后每天还必须往回看t页 ...

  4. 去掉vs2010字符串下红色波浪线

    由于在vs集成了qt库,无法提升代码. 所以下载了visual assist,然后新的问题出现了,凡是在vs中输入的字符串,下面都有红色的波浪线,而且没有错误,只是看着不舒服. 解决方法: 在VAss ...

  5. Nutch1.6学习笔记

    回 到 目 录 暑假每天傍晚或晚上更新 伪恋赛高 这里提供nutch1.6的src下载: apache-nutch-1.6-src.zip 115网盘礼包码:5lbcymlo6u76http://11 ...

  6. 怎样查看电脑的IP地址

    在DOW窗体 :cmd->ipconfig 见截图:

  7. [问题]HDOJ1032 The 3n + 1 problem

    http://acm.hdu.edu.cn/showproblem.php? pid=1032 这题能够用暴力求解.求出在[ni,nj]之间全部数字产生的最大值. 通过观察能够知道,当nk靠近nj的时 ...

  8. c++开源爬虫-Larbin简单介绍

    原文地址:http://leihuang.net/2014/06/16/Larbin-Introduction/ 由于近期学校实训.做的是一个搜索相关的项目,而且是c++的一个项目.所以就想到了lar ...

  9. Light OJ 1341 Aladdin and the Flying Carpet

    题意:求大于b的a的因数对有几组.例10  2结果为{2,5},12 2结果为{2,6}{3,4}-----不反复 解一:分解质因数+DFS #include <iostream> #in ...

  10. Android笔记三十四.Service综合实例二

    综合实例2:client訪问远程Service服务 实现:通过一个button来获取远程Service的状态,并显示在两个文本框中. 思路:如果A应用须要与B应用进行通信,调用B应用中的getName ...