React组件之高阶函数替换Mixins注入 · Web前端爱好者
实现功能基于ES6用React实现一个倒计时组件,格式:6时6分6秒,然后每隔一秒不断倒计时。
回顾Mixins当然,完全可以直接写在一个React.createClass函数里面,但可复用性不高,所以换做在之前官方推荐的做法就是利用Mixins特性,所谓Mixins我理解就是注入,类似于Java Spring的依赖注入,简单代码如下:
// 定义一个倒计时Mixinsvar SetIntervalMixin = { componentWillMount: function() { this.intervals = []; }, setInterval: function() { this.intervals.push(setInterval.apply(null, arguments)); }, componentWillUnmount: function() { this.intervals.forEach(clearInterval); }};// 注入Mixins到组件中var Timer = R...阅读全文
回顾Mixins当然,完全可以直接写在一个React.createClass函数里面,但可复用性不高,所以换做在之前官方推荐的做法就是利用Mixins特性,所谓Mixins我理解就是注入,类似于Java Spring的依赖注入,简单代码如下:
// 定义一个倒计时Mixinsvar SetIntervalMixin = { componentWillMount: function() { this.intervals = []; }, setInterval: function() { this.intervals.push(setInterval.apply(null, arguments)); }, componentWillUnmount: function() { this.intervals.forEach(clearInterval); }};// 注入Mixins到组件中var Timer = R...阅读全文