MCPcopy Create free account
hub / github.com/tomchentw/react-google-maps / withScriptjs

Function withScriptjs

src/withScriptjs.jsx:12–85  ·  view source on GitHub ↗
(BaseComponent)

Source from the content-addressed store, hash-verified

10const LOADING_STATE_LOADED = `LOADED`
11
12export function withScriptjs(BaseComponent) {
13 const factory = React.createFactory(BaseComponent)
14
15 class Container extends React.PureComponent {
16 static displayName = `withScriptjs(${getDisplayName(BaseComponent)})`
17
18 static propTypes = {
19 loadingElement: PropTypes.node.isRequired,
20 googleMapURL: PropTypes.string.isRequired,
21 }
22
23 state = {
24 loadingState: LOADING_STATE_NONE,
25 }
26
27 isUnmounted = false
28
29 handleLoaded = _.bind(this.handleLoaded, this)
30
31 handleLoaded() {
32 if (this.isUnmounted) {
33 return
34 }
35 this.setState({
36 loadingState: LOADING_STATE_LOADED,
37 })
38 }
39
40 componentWillMount() {
41 const { loadingElement, googleMapURL } = this.props
42 invariant(
43 !!loadingElement && !!googleMapURL,
44 `Required props loadingElement or googleMapURL is missing. You need to provide both of them.`
45 )
46 }
47
48 componentDidMount() {
49 const { loadingState } = this.state
50 if (loadingState !== LOADING_STATE_NONE || !canUseDOM) {
51 return
52 }
53 this.setState({
54 loadingState: LOADING_STATE_BEGIN,
55 })
56 // Don't load scriptjs as a dependency since we do not want this module be used on server side.
57 // eslint-disable-next-line global-require
58 const scriptjs = require(`scriptjs`)
59 const { googleMapURL } = this.props
60 scriptjs(googleMapURL, this.handleLoaded)
61 }
62
63 componentWillUnmount() {
64 this.isUnmounted = true
65 }
66
67 render() {
68 const {
69 loadingElement,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected