-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello-react.js
77 lines (58 loc) · 1.88 KB
/
hello-react.js
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
'use strict';
const el = React.createElement;
// A simple component
class HelloWorld extends React.Component {
render() {
return el('h1', {style: {color: 'gray', textAlign: 'center'}}, 'Hi ' + this.props.inputData + '!');
}
}
const domContainer1 = document.getElementById('react-example-one');
ReactDOM.render(el(HelloWorld, {inputData: 'Learner'}),
domContainer1);
// A stateful component
class StatefulComponent extends React.Component {
constructor(props) {
super(props);
this.state = {activeIndex: -1};
this.tabContents = ['tab-1 content', 'tab-2 content', 'tab-3 content'];
}
tabClicked(index) {
// this.setState( state => (
// { tabClicked: !state.tabClicked }
// ) );
let newIndex = index;
if (this.state.activeIndex === index) {
newIndex = -1;
}
this.setState({activeIndex: newIndex});
}
tabHeadingClass(index) {
if (index === this.state.activeIndex)
return 'active';
return '';
}
tabContentClass(index) {
if (index === this.state.activeIndex)
return 'show';
return 'hide';
}
// componentWillUnmount()
// {
// this.setState({ tabClicked : 0 });
// }
render() {
const tabs = [];
for (let i = 0; i < 3; i++) {
tabs.push(
el('div', {className: `row-tab-expanded`, key: 'tab-' + i},
el('h1', {className: `tab-heading ${this.tabHeadingClass(i)}`, onClick: () => this.tabClicked(i)}, 'Tab ' + i),
el('div', {className: `tab-content ${this.tabContentClass(i)}`}, this.tabContents[i]),
)
);
}
return tabs;
}
}
const domTabContainers2 = document.getElementById('react-example-two');
ReactDOM.render(el(StatefulComponent),
domTabContainers2);