-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.js
94 lines (79 loc) · 3.17 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
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
import React from 'react'
import ReactDOM from 'react-dom'
import { componentDidMount, componentWillMount, componentWillUnmount, componentDidUpdate,
componentWillReceiveProps, shouldComponentUpdate, getInitialState,
propTypes, displayName, combine, addSpecs, createClass, 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 })
}),
displayName('Component2'),
displayName('Test')
)(Hello)
const withSpecs = addSpecs({
displayName: 'test',
getInitialState: () => ({ bar: 'bar' }),
getDefaultProps: () => ({ baz: 'baz' }),
propTypes: { received: React.PropTypes.number.isRequired },
mixins: [{ mixinVar: true }],
statics: { staticMethod: () => true },
componentWillMount: (self) => self.setState({ foo: 'foobar' })
})
const withSpecs2 = createClass({
mixins: [{ mixinVar2: true }],
statics: { staticMethod2: () => false },
componentWillMount: (self) => console.log(self),
})
const Component3 = combine(withSpecs, withSpecs2)(StatelessComponent)
const Component4 = componentDidMount(
(self) => self.setState({ foo: 'foo' }),
componentDidMount(
(self) => self.setState({ bar: 'bar' })
)
)(StatelessComponent)
//const Component5 = componentDidMount((self) => self.setState({ bar: 'bar' }), Component4)
/**
* 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} />
<br /><br /><br />
<Component4 />
</div>
)
}
})
ReactDOM.render(React.createElement(Wrapper), div)