-
Notifications
You must be signed in to change notification settings - Fork 0
/
example3-mixins.html
117 lines (106 loc) · 2.94 KB
/
example3-mixins.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>example3</title>
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script src="build/browser.js"></script>
<script src="build/jquery.js"></script>
<script src="build/remarkable.js"></script>
</head>
<body>
<div id="example"> </div>
<div id="example2"> </div>
<div id="example4"> </div>
<script type="text/babel">
var SetIntervalMixin = {
componentWillMount: function() {
this.intervals = [];
},
setInterval: function() {
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function() {
this.intervals.forEach(clearInterval);
}
};
var TickTock = React.createClass({
mixins: [SetIntervalMixin], // Use the mixin
getInitialState: function() {
return {seconds: 0};
},
componentDidMount: function() {
this.setInterval(this.tick, 1000); // Call a method on the mixin
},
tick: function() {
this.setState({seconds: this.state.seconds + 1});
},
render: function() {
return (
<p>
React has been running for {this.state.seconds} seconds.
</p>
);
}
});
ReactDOM.render(
<TickTock />,
document.getElementById('example')
);
// function FancyCheckbox(props) {
// var fancyClass = props.checked ? 'FancyChecked' : 'FancyUnchecked';
// return (
// <div className={fancyClass} onClick={props.onClick}>
// {props.children}
// </div>
// );
// }
// ReactDOM.render(
// <FancyCheckbox checked={true} onClick={console.log.bind(console)}>
// Hello world!
// </FancyCheckbox>,
// document.getElementById('example2')
// );
// function FancyCheckbox(props) {
// var { checked, ...other } = props;
// var fancyClass = checked ? 'FancyChecked' : 'FancyUnchecked';
// <div {...other} className={fancyClass} />
// );
// }
// ReactDOM.render(
// <FancyCheckbox checked={true} onClick={console.log.bind(console)}>
// Hello world!
// </FancyCheckbox>,
// document.getElementById('example2')
// );
//
var MyComponent = React.createClass({
handleClick: function() {
// Explicitly focus the text input using the raw DOM API.
if (this.myTextInput !== null) {
this.myTextInput.focus();
}
},
render: function() {
// The ref attribute is a callback that saves a reference to the
// component to this.myTextInput when the component is mounted.
return (
<div>
<input type="text" ref={(ref) => this.myTextInput = ref} />
<input
type="button"
value="Focus the text input"
onClick={this.handleClick}
/>
</div>
);
}
});
ReactDOM.render(
<MyComponent />,
document.getElementById('example4')
);
</script>
</body>
</html>