-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (128 loc) · 2.93 KB
/
index.js
File metadata and controls
139 lines (128 loc) · 2.93 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Menu extends React.Component {
constructor(props) {
super(props);
this.state = {
active: 0,
};
}
clickedOn(active) {
this.setState({ active });
}
render() {
const activeItem = this.props.menuItems[this.state.active];
const tabs = this.props.menuItems.map((item, i) => (
<li
key={i}
className={(this.state.active === i)
? 'menu__item menu__item--active'
: 'menu__item'}
onClick={() => this.clickedOn(i)}
>{item}</li>
));
return (
<nav>
<ul className="menu">
{tabs}
</ul>
<div className="info">Active menu item: {activeItem}</div>
</nav>
);
}
}
Menu.propTypes = {
menuItems: React.PropTypes.arrayOf(
React.PropTypes.string.isRequired,
).isRequired,
};
ReactDOM.render(
<Menu menuItems={['Home', 'Services', 'About Us', 'Contact Us']} />,
document.getElementById('menu')
);
const textToArray = str => str.split('').map(a => a.charCodeAt(0));
class Hello extends React.Component {
constructor(props) {
super(props);
this.start = this.props.word.replace(/[a-z]/gi, 'A');
this.state = {
code: textToArray(this.start),
text: this.start,
};
this.changeLetter = this.changeLetter.bind(this);
}
componentDidMount() {
this.intervalID = setInterval(this.changeLetter, 50);
}
componentWillUnmount() {
clearInterval(this.intervalID);
}
changeLetter() {
let code = this.state.code;
let text = this.state.text;
if (this.props.word === text) {
clearInterval(this.intervalID);
}
for (let i = 0; i < this.props.word.length; i++) {
if (this.props.word.charCodeAt(i) !== code[i]) {
code[i] += 1;
break;
}
}
text = code.map(a => String.fromCharCode(a)).join('');
this.setState({
code,
text,
});
}
render() {
return (
<p className="hello">
{this.state.text}
</p>
);
}
}
Hello.propTypes = {
word: React.PropTypes.string.isRequired,
};
ReactDOM.render(
<Hello word="HELLO REACTJS!" />,
document.getElementById('hello')
);
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = {
elapsed: 0,
};
}
// called when component has been rendered
componentDidMount() {
this.timer = setInterval(() => this.tick(), 50);
}
// called right before the component is removed and destroyed
componentWillUnmount() {
clearInterval(this.timer);
}
tick() {
this.setState({
elapsed: new Date() - this.props.start,
});
}
render() {
const elapsed = Math.round(this.state.elapsed / 100);
const seconds = (elapsed / 10).toFixed(1);
return (
<p>This timer was started {seconds} secs ago.</p>
);
}
}
Timer.defaultProps = {
start: Date.now(),
};
Timer.propTypes = {
start: React.PropTypes.number,
};
ReactDOM.render(
<Timer />,
document.getElementById('timer')
);