Mixins 改成使用高阶组件调用
把组件放在另外一个组件的 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>
参考地址:
Mixins Are Dead. Long Live Composition
Mixins 改成使用高阶组件调用的更多相关文章
- 浅析为什么用高阶组件代替 Mixins
转载来源 感谢分享 Mixins 引入了无形的依赖 应尽量构建无状态组件,Mixin 则反其道而行之 Mixin 可能会相互依赖,相互耦合,不利于代码维护 不同的 Mixin 中的方法可能会相互冲突 ...
- 聊聊React高阶组件(Higher-Order Components)
使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低 ...
- 高阶组件(Higher-Order Components)
有时候人们很喜欢造一些名字很吓人的名词,让人一听这个名词就觉得自己不可能学会,从而让人望而却步.但是其实这些名词背后所代表的东西其实很简单. 我不能说高阶组件就是这么一个东西.但是它是一个概念上很简单 ...
- 当初要是看了这篇,React高阶组件早会了
当初要是看了这篇,React高阶组件早会了. 概况: 什么是高阶组件? 高阶部件是一种用于复用组件逻辑的高级技术,它并不是 React API的一部分,而是从React 演化而来的一种模式. 具体地说 ...
- react_结合 redux - 高阶函数 - 高阶组件 - 前端、后台项目打包运行
Redux 独立的集中式状态管理 js 库 - 参见 My Git 不是 react 库,可以与 angular.vue 配合使用,通常和 react 用 yarn add redux import ...
- React文档(二十四)高阶组件
高阶组件(HOC)是React里的高级技术为了应对重用组件的逻辑.HOCs本质上不是React API的一部分.它是从React的组合性质中显露出来的模式. 具体来说,一个高阶组件就是一个获取一个组件 ...
- React进阶之高阶组件
前言 本文代码浅显易懂,思想深入实用.此属于react进阶用法,如果你还不了解react,建议从文档开始看起. 我们都知道高阶函数是什么, 高阶组件其实是差不多的用法,只不过传入的参数变成了react ...
- react高阶组件
高阶组件 为了提高组件复用性,在react中就有了HOC(Higher-Order Component)的概念.所谓的高阶组件,其本质依旧是组件,只是它返回另外一个组件,产生新的组件可以对属性进行包装 ...
- 【转】react的高阶组件
React进阶之高阶组件 前言 本文代码浅显易懂,思想深入实用.此属于react进阶用法,如果你还不了解react,建议从文档开始看起. 我们都知道高阶函数是什么, 高阶组件其实是差不多的用法,只 ...
随机推荐
- Asp.net Web Api 2 FORM Authentication Demo
最近看了一点 web api 2方面的书,对认证都是简单介绍了下,所以我在这里做个简单Demo,本文主要是FORM Authentication,顺带把基本认证也讲了. Demo 一.FORM Aut ...
- JNLP Slave connection error解决办法
Replace in jnlp-file <argument>-workDir</argument> <argument /> with <argume ...
- Jenkins有用的插件
1. Multijob plugin: 多个任务同时运行 2. ssh slave plugin: 用于安装slave? Allows to launch over ssh, using a java ...
- 透彻理解Spring事务设计思想之手写实现(山东数漫江湖)
前言 事务,是描述一组操作的抽象,比如对数据库的一组操作,要么全部成功,要么全部失败.事务具有4个特性:Atomicity(原子性),Consistency(一致性),Isolation(隔离性),D ...
- spring 那点事
Spring核心功能 DI(IOC) 何谓DI(IOC) DI(依赖注入)是spring的核心功能之一. Dependency Injection 和 Inversion of Control 其实就 ...
- HDU 1070 Milk (模拟)
题目链接 Problem Description Ignatius drinks milk everyday, now he is in the supermarket and he wants to ...
- 【HNOI】 攻城略池 tree-dp
[题目大意] 给定一棵树,边有边权,每个节点有一些兵,现在叶子节点在0时刻被占领,并且任意节点在x被占领,那么从x+1开始,每单位时间产生一个兵,兵会顺着父亲节点一直走到根(1),其中每经过一个节点, ...
- 理解js中私有变量
私有变量在js中是个什么概念.当下我的认识是var所定义的变量,实际可以理解为属性和方法,或者单单是临时存储器,不归属任何对象. 一个声明函数: function a(){ var v = &quo ...
- eclipse+EGIT+GitHub
下载EGIT:http://wiki.eclipse.org/EGit/FAQ#Where_can_I_find_older_releases_of_EGit.3F 1.下载eclipse版本对应的E ...
- 【Sqlite3】sqlite_sequence表(转)
sqlite_sequence表也是SQLite的系统表.该表用来保存其他表的RowID的最大值.数据库被创建时,sqlite_sequence表会被自动创建.该表包括两列.第一列为name,用来存储 ...