-
Notifications
You must be signed in to change notification settings - Fork 192
/
App.js
47 lines (37 loc) · 1.47 KB
/
App.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
var createReactClass = require('create-react-class')
var DOM = require('react-dom-factories')
var div = DOM.div, button = DOM.button, ul = DOM.ul, li = DOM.li
// This is just a simple example of a component that can be rendered on both
// the server and browser
module.exports = createReactClass({
// We initialise its state by using the `props` that were passed in when it
// was first rendered. We also want the button to be disabled until the
// component has fully mounted on the DOM
getInitialState: function() {
return {items: this.props.items, disabled: true}
},
// Once the component has been mounted, we can enable the button
componentDidMount: function() {
this.setState({disabled: false})
},
// Then we just update the state whenever its clicked by adding a new item to
// the list - but you could imagine this being updated with the results of
// AJAX calls, etc
handleClick: function() {
this.setState({
items: this.state.items.concat('Item ' + this.state.items.length),
})
},
// For ease of illustration, we just use the React JS methods directly
// (no JSX compilation needed)
// Note that we allow the button to be disabled initially, and then enable it
// when everything has loaded
render: function() {
return div(null,
button({onClick: this.handleClick, disabled: this.state.disabled}, 'Add Item'),
ul({children: this.state.items.map(function(item) {
return li(null, item)
})})
)
},
})