-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (39 loc) · 1.4 KB
/
index.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
import React from 'react'
export const initialAction = {type: '__init__'}
export default function Stateful (WrappedComponent, reducer) {
class StatefulComponent extends React.Component {
constructor (props) {
super(props)
if (typeof reducer !== 'function' && reducer.length !== 2) {
throw new Error('reducer needs to be a function of shape (state, action) => state')
}
this.state = reducer(undefined, initialAction)
}
dispatch (action) {
this.setState(state => reducer(state, action))
}
render () {
if (defined(this.props.dispatch)) {
console.warn(`${StatefulComponent.displayName}: You provided a "dispatch" prop. This will be ignored. Remove the prop to get rid of this message.`)
}
if (defined(this.props.state)) {
console.warn(`${StatefulComponent.displayName}: You provided a "state" prop. This will be ignored. Remove the prop to get rid of this message.`)
}
return (
<WrappedComponent
{...this.props}
dispatch={this.dispatch.bind(this)}
state={this.state}
/>
)
}
}
StatefulComponent.displayName = `Stateful(${getComponentDisplayName(WrappedComponent)})`
return StatefulComponent
}
function getComponentDisplayName (Component) {
return Component.displayName || Component.name || 'Unknown'
}
function defined (x) {
return typeof x !== 'undefined'
}