把组件放在另外一个组件的 render 方法里面, 并且利用了 {...this.props} {...this.state} 这些  JSX 展开属性

对比下2种代码:

原始方式:

<!DOCTYPE html>
<html>
<head>
<script src="../build/react.js"></script>
<script src="../build/react-dom.js"></script>
<script src="../build/browser.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
var intervalMixin = {
componentDidMount: function(){
this.arr = [];
},
setInterval: function(callback, interval){
var token = setInterval(callback, interval);
this.arr.push(token);
return token;
}
}
var S = React.createClass({
mixins: [intervalMixin],
getInitialState: function(){
return {
num: 0
}
},
componentDidMount: function(){
this.setInterval( ()=> this.setState({num: this.state.num+1}), 1000);
},
render: function(){
return <div>
{this.state.num}
</div>
}
});
ReactDOM.render(
<S q="bb" />,
document.getElementById('example') );
</script>
</body>
</html>

高阶组件调用:

<!DOCTYPE html>
<html>
<head>
<script src="../build/react.js"></script>
<script src="../build/react-dom.js"></script>
<script src="../build/browser.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
// 注意参数 Com 需要第1个字母大写
const Enhance = Com => {
const Res = class extends React.Component {
// 这种写法 ES6 不支持
// static defaultProps = {
// autoPlay: false,
// maxLoops: 10,
// }; // 注意这里有分号 constructor(props, context) {
super(props, context);
this.state = {
time: 0
};
} static a(){ } componentDidMount() {
setInterval(this.tick.bind(this), 1000);
} tick() {
this.setState({
time: this.state.time + 1
});
} render() {
// ...this.props 相当于 text = {this.props.text}
// ...this.state 相当于 time = {this.state.time}
return <Com {...this.props} {...this.state} />;
}
} Res.defaultProps = {
text: 'hello world'
}; return Res;
} class Time extends React.Component {
render() {
return <div>
{this.props.text} {this.props.time}
</div>;
}
} /*
注意这里 A 的值必须为大写,
不能写成 这种形式
ReactDOM.render(
Enhance(Time),
document.getElementById('example') );
)
*/
var A = Enhance(Time);
ReactDOM.render(
<A />,
document.getElementById('example') ); </script>
</body>
</html>

参考地址:

传递 Props

Mixins Are Dead. Long Live Composition

Mixins 改成使用高阶组件调用的更多相关文章

  1. 浅析为什么用高阶组件代替 Mixins

    转载来源 感谢分享 Mixins 引入了无形的依赖 应尽量构建无状态组件,Mixin 则反其道而行之 Mixin 可能会相互依赖,相互耦合,不利于代码维护 不同的 Mixin 中的方法可能会相互冲突 ...

  2. 聊聊React高阶组件(Higher-Order Components)

    使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低 ...

  3. 高阶组件(Higher-Order Components)

    有时候人们很喜欢造一些名字很吓人的名词,让人一听这个名词就觉得自己不可能学会,从而让人望而却步.但是其实这些名词背后所代表的东西其实很简单. 我不能说高阶组件就是这么一个东西.但是它是一个概念上很简单 ...

  4. 当初要是看了这篇,React高阶组件早会了

    当初要是看了这篇,React高阶组件早会了. 概况: 什么是高阶组件? 高阶部件是一种用于复用组件逻辑的高级技术,它并不是 React API的一部分,而是从React 演化而来的一种模式. 具体地说 ...

  5. react_结合 redux - 高阶函数 - 高阶组件 - 前端、后台项目打包运行

    Redux 独立的集中式状态管理 js 库 - 参见 My Git 不是 react 库,可以与 angular.vue 配合使用,通常和 react 用 yarn add redux import ...

  6. React文档(二十四)高阶组件

    高阶组件(HOC)是React里的高级技术为了应对重用组件的逻辑.HOCs本质上不是React API的一部分.它是从React的组合性质中显露出来的模式. 具体来说,一个高阶组件就是一个获取一个组件 ...

  7. React进阶之高阶组件

    前言 本文代码浅显易懂,思想深入实用.此属于react进阶用法,如果你还不了解react,建议从文档开始看起. 我们都知道高阶函数是什么, 高阶组件其实是差不多的用法,只不过传入的参数变成了react ...

  8. react高阶组件

    高阶组件 为了提高组件复用性,在react中就有了HOC(Higher-Order Component)的概念.所谓的高阶组件,其本质依旧是组件,只是它返回另外一个组件,产生新的组件可以对属性进行包装 ...

  9. 【转】react的高阶组件

    React进阶之高阶组件   前言 本文代码浅显易懂,思想深入实用.此属于react进阶用法,如果你还不了解react,建议从文档开始看起. 我们都知道高阶函数是什么, 高阶组件其实是差不多的用法,只 ...

随机推荐

  1. free命令buff和cache的区别

    [root@iz8vb8kca7twx6177bqg0fz ~]# free -h               total        used        free      shared  b ...

  2. Unity下的ECS框架 Entitas简介

    最近随着守望先锋制作组在gdc上发布的一个关于ecs的talk,ecs这个架构算是得到了一定的曝光度. 在这之前,github上就一直有一个C#的ecs框架名为Entitas,截止现在已经有1300+ ...

  3. Fire! (双bfs+预处理)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. 双内网渗透代理之reGeorg+Proxifier

    由于这个工具第一次体验感觉还不错,很稳定.因此在这记录一下reGeorg+Proxifier的配置及其使用. 下载地址 :https://github.com/sensepost/reGeorg.gi ...

  5. Shell脚本 - nginx启动脚本

    OS:CentOS/Redhat 系列 并在 Centos 6.7 和 Centos 7.2 上测试正常 #!/bin/bash # # auth: daxin # time: 2018/07/10 ...

  6. map,set的底层实现:红黑树[多图,手机慎入]

    最近天下有一种颇不太平的感觉,各地的乱刀砍人,到处是贪官服法.京东准备上市了,阿里最近也提交申请了,猎豹也逆袭了,据说猎豹移动在国际市场上表现甚是抢眼.只有屌丝还在写着代码.花开花又谢,花谢花又开,为 ...

  7. 【uva10779】收集者的难题

    按照题意建模就行了. #include<bits/stdc++.h> #define naive 0 #define inf 1000000007 using namespace std; ...

  8. 【bzoj4551】TJOI2016&HEOI2016树

    这题嘛…… 子树询问什么的,直接dfs序线段树无脑写,是吧…… 然后几分钟之内zcy就写出了这样的东西: #include<bits/stdc++.h> #define N 100005 ...

  9. webapi-2 接口参数

    1. 实例 using System; using System.Collections.Generic; using System.Linq; using System.Net; using Sys ...

  10. android intent 传数据

    1. 基本数据类型 Intent intent = new Intent(); intent.setClass(activity1.this, activity2.class); //描述起点和目标 ...