forked from cerebroapp/cerebro-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Preload.js
39 lines (36 loc) · 934 Bytes
/
Preload.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
import React, { Component } from 'react'
/**
* Component that renders child function only after props.promise is resolved or rejected
* You can provide props.loader that will be rendered before
*/
class Preload extends Component {
constructor(props) {
super(props)
this.state = {
result: null,
error: null
}
}
componentDidMount() {
this.props.promise
.then(result => this.setState({ result }))
.catch(error => this.setState({ error }))
}
render() {
const { loader, children } = this.props
const { result, error } = this.state
if (result || error) {
return children(result, error)
}
return loader || null
}
}
Preload.propTypes = {
loader: React.PropTypes.element,
children: React.PropTypes.func.isRequired,
promise: React.PropTypes.shape({
then: React.PropTypes.func,
catch: React.PropTypes.func
}).isRequired
}
export default Preload