-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.js
65 lines (56 loc) · 2.3 KB
/
dev.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
import React from 'react'
import ReactDOM from 'react-dom'
import { componentDidMount, componentWillMount, componentWillUnmount, componentDidUpdate,
componentWillReceiveProps, shouldComponentUpdate, getInitialState,
propTypes, displayName, combine, addSpecs, onPropChange } from './src/index.js'
const updateOnPropChange = (customProps = '') => {
const propsToCheck = customProps.split(' ')
return shouldComponentUpdate((nextProps, nextState, props) => {
const changedProps = (propsToCheck || Object.keys(nextProps)).filter((prop) =>
nextProps[prop] !== props[prop]).length
return changedProps
})
}
const StatelessComponent = (props) => <div>{JSON.stringify(props)}</div>
const Hello = (props) => <h1>Hello {props.world} {props.smiley} {props.test} {props.received}</h1>
const Component1 = combine(updateOnPropChange('foo'))(StatelessComponent)
const Component2 = combine(
componentDidMount((self, props, state) => {
setTimeout(() => self.setState({ smiley: ':(' }), 1000)
setTimeout(() => self.setState({ smiley: ':)' }), 2000)
}),
shouldComponentUpdate((self, nextProps, nextState) => nextState.smiley === ':)'),
componentDidMount((self, props, state) => self.setState({ test: 'test' })),
onPropChange({
received: (self, props) => self.setState({ received: props.received * 2 })
})
)(Hello)
const Component3 = addSpecs({
componentWillMount: (self) => self.setState({ foo: 'foobar' })
})(StatelessComponent)
/**
* Append div to document and render the dev app
*/
const div = document.createElement('div')
document.body.appendChild(div)
const Wrapper = React.createClass({
getInitialState() {
return {
received: 0
}
},
componentDidMount() {
setTimeout(() => this.setState({ received: this.state.received + 1, foo: 'foo' }), 1000)
setTimeout(() => this.setState({ received: this.state.received + 1 }), 2000)
setTimeout(() => this.setState({ received: this.state.received + 1, foo: 'bar' }), 3000)
},
render() {
return (
<div>
<Component2 world="world" received={this.state.received} />
<Component3 received={this.state.received} />
</div>
)
}
})
ReactDOM.render(React.createElement(Wrapper), div)