-
Notifications
You must be signed in to change notification settings - Fork 936
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(async/ScriptjsLoader): replacement of async/ScriptjsGoogleMap
* Mark async/ScriptjsGoogleMap as deprecated * Closes #145 * Ref #92 BREAKING CHANGE: migrate from async/ScriptjsGoogleMap to async/ScriptjsLoader and changed its behavior from implicit inheritance to simple delegation To migrate the code follow the example below (extracted from examples/gh-pages migration): Before: ```js <ScriptjsLoader hostname={"maps.googleapis.com"} pathname={"/maps/api/js"} query={{v: `3.exp`, libraries: "geometry,drawing,places"}} // // <GoogleMap> props defaultZoom={3} defaultCenter={{lat: -25.363882, lng: 131.044922}} onClick={::this._handle_map_click} /> ``` After: ```js <ScriptjsLoader hostname={"maps.googleapis.com"} pathname={"/maps/api/js"} query={{v: `3.exp`, libraries: "geometry,drawing,places"}} // googleMapElement={ <GoogleMap defaultZoom={3} defaultCenter={{lat: -25.363882, lng: 131.044922}} onClick={::this._handle_map_click} /> } /> ```
- Loading branch information
Showing
2 changed files
with
92 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { | ||
default as React, | ||
Component, | ||
PropTypes, | ||
} from "react"; | ||
|
||
import { | ||
default as canUseDOM, | ||
} from "can-use-dom"; | ||
|
||
import { | ||
default as warning, | ||
} from "warning"; | ||
|
||
import { | ||
GoogleMap, | ||
} from "../index"; | ||
|
||
import { | ||
default as makeUrl, | ||
urlObjDefinition, | ||
getUrlObjChangedKeys, | ||
} from "../utils/makeUrl"; | ||
|
||
export default class ScriptjsLoader extends Component { | ||
static propTypes = { | ||
...urlObjDefinition, | ||
// PropTypes for ScriptjsLoader | ||
loadingElement: PropTypes.node, | ||
googleMapElement: PropTypes.element.isRequired, | ||
} | ||
|
||
state = { | ||
isLoaded: false, | ||
} | ||
|
||
componentWillMount () { | ||
if (!canUseDOM) { | ||
return; | ||
} | ||
/* | ||
* External commonjs require dependency -- begin | ||
*/ | ||
const scriptjs = require("scriptjs"); | ||
/* | ||
* External commonjs require dependency -- end | ||
*/ | ||
const {protocol, hostname, port, pathname, query} = this.props; | ||
const urlObj = {protocol, hostname, port, pathname, query}; | ||
const url = makeUrl(urlObj); | ||
scriptjs(url, () => this.setState({ isLoaded: true })); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tomchentw
Author
Owner
|
||
} | ||
|
||
componentWillReceiveProps (nextProps) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
const changedKeys = getUrlObjChangedKeys(this.props, nextProps); | ||
|
||
warning(0 === changedKeys.length, `ScriptjsLoader doesn't support mutating url related props after initial render. Changed props: %s`, `[${ changedKeys.join(", ") }]`); | ||
} | ||
} | ||
|
||
render () { | ||
if (this.state.isLoaded) { | ||
return this.props.googleMapElement; | ||
} else { | ||
return this.props.loadingElement; | ||
} | ||
} | ||
} |
What if somebody add two SciptjsLoaders as sibling? Will both load google maps api? Will it not break the app?