React第九讲课堂笔记(mixin)

mixin 是一个普通对象,通过 mixin 可以在不同组件间共享代码,使你的React程序变得更为可重用。

注意,ES6语法不支持mixin写法,而是可以通过decorator去实现代码共享,这里使用ES5语法做示例说明。

1. ES5 语法实现 mixin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

import React from 'react';

var SetIntervalMixin = {
componentWillMount: function() {
this.intervals = [];
},
setInterval: function() {
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function() {
this.intervals.forEach(clearInterval);
}
};

var MixinDemo = React.createClass({
// Use the mixin
mixins: [SetIntervalMixin],
getInitialState: function() {
return {seconds: 0};
},
componentDidMount: function() {
// Call a method on the mixin
this.setInterval(this.tick, 1000);
},
tick: function() {
this.setState({seconds: this.state.seconds + 1});
},
render: function() {
return (
<p>
计时器已经运行了: {this.state.seconds} 秒.
</p>
);
}
});

export default MixinDemo;

2. 用high-order component的方式实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { Component } from 'react';

let Mixin = MixinComponent => class extends Component {
constructor() {
super();
this.state = { val: 0 };
this.update = this.update.bind(this);
}
update(){
this.setState({val: this.state.val + 1});
}
componentWillMount(){
console.log('will mount...')
}
render(){
return (
<MixinComponent
update={this.update}
{...this.state}
{...this.props}
/>
)
}
componentDidMount(){
console.log('mounted...')
}
}

const Button = (props) => {
return (
<button onClick={props.update}>
{props.txt} - {props.val}
</button>
)
}

const Label = (props) => {
return (
<label onMouseMove={props.update}>
{props.txt} - {props.val}
</label>
)
}

let ButtonMixed = Mixin(Button);
let LabelMixed = Mixin(Label);

class Mixins extends Component {
render(){
return (
<div>
<ButtonMixed txt="button" />
<LabelMixed txt="label" />
</div>
)
}
}

export default Mixins;