Skip to content

Commit ccfadd4

Browse files
committed
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} /> } /> ```
1 parent 0a301c4 commit ccfadd4

File tree

2 files changed

+92
-42
lines changed

2 files changed

+92
-42
lines changed

src/async/ScriptjsGoogleMap.js

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import {
22
default as React,
33
Component,
4-
PropTypes,
54
} from "react";
65

7-
import {
8-
default as canUseDOM,
9-
} from "can-use-dom";
10-
116
import {
127
default as warning,
138
} from "warning";
@@ -17,49 +12,35 @@ import {
1712
} from "../index";
1813

1914
import {
20-
default as makeUrl,
21-
urlObjDefinition,
22-
getUrlObjChangedKeys,
23-
} from "../utils/makeUrl";
15+
default as ScriptjsLoader,
16+
} from "./ScriptjsLoader";
2417

2518
export default class ScriptjsGoogleMap extends Component {
26-
static propTypes = {
27-
...urlObjDefinition,
28-
// PropTypes for ScriptjsGoogleMap
29-
loadingElement: PropTypes.node,
30-
}
31-
32-
state = {
33-
isLoaded: false,
34-
}
3519

3620
componentWillMount () {
37-
if (!canUseDOM) {
38-
return;
39-
}
40-
const scriptjs = require("scriptjs");
41-
const {protocol, hostname, port, pathname, query, ...restProps} = this.props;
42-
const urlObj = {protocol, hostname, port, pathname, query};
43-
const url = makeUrl(urlObj);
44-
scriptjs(url, () => this.setState({ isLoaded: true }));
45-
}
46-
47-
componentWillReceiveProps (nextProps) {
48-
if ("production" !== process.env.NODE_ENV) {
49-
const changedKeys = getUrlObjChangedKeys(this.props, nextProps);
50-
51-
warning(0 === changedKeys.length, `ScriptjsGoogleMap doesn't support mutating url related props after initial render. Changed props: %s`, `[${ changedKeys.join(", ") }]`);
52-
}
21+
warning(false,
22+
`"async/ScriptjsGoogleMap" is deprecated now and will be removed in next major release (5.0.0). Use "async/ScriptjsLoader" instead.
23+
See https://github.com/tomchentw/react-google-maps/pull/150 for more details.`
24+
);
5325
}
5426

5527
render () {
56-
if (this.state.isLoaded) {
57-
const {protocol, hostname, port, pathname, query, ...restProps} = this.props;
58-
return (
59-
<GoogleMap {...restProps} />
60-
);
61-
} else {
62-
return this.props.loadingElement;
63-
}
28+
const {protocol, hostname, port, pathname, query, loadingElement, children, ...restProps} = this.props;
29+
30+
return (
31+
<ScriptjsLoader
32+
protocol={protocol}
33+
hostname={hostname}
34+
port={port}
35+
pathname={pathname}
36+
query={query}
37+
loadingElement={loadingElement}
38+
googleMapElement={
39+
<GoogleMap {...restProps}>
40+
{children}
41+
</GoogleMap>
42+
}
43+
/>
44+
)
6445
}
6546
}

src/async/ScriptjsLoader.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {
2+
default as React,
3+
Component,
4+
PropTypes,
5+
} from "react";
6+
7+
import {
8+
default as canUseDOM,
9+
} from "can-use-dom";
10+
11+
import {
12+
default as warning,
13+
} from "warning";
14+
15+
import {
16+
GoogleMap,
17+
} from "../index";
18+
19+
import {
20+
default as makeUrl,
21+
urlObjDefinition,
22+
getUrlObjChangedKeys,
23+
} from "../utils/makeUrl";
24+
25+
export default class ScriptjsLoader extends Component {
26+
static propTypes = {
27+
...urlObjDefinition,
28+
// PropTypes for ScriptjsLoader
29+
loadingElement: PropTypes.node,
30+
googleMapElement: PropTypes.element.isRequired,
31+
}
32+
33+
state = {
34+
isLoaded: false,
35+
}
36+
37+
componentWillMount () {
38+
if (!canUseDOM) {
39+
return;
40+
}
41+
/*
42+
* External commonjs require dependency -- begin
43+
*/
44+
const scriptjs = require("scriptjs");
45+
/*
46+
* External commonjs require dependency -- end
47+
*/
48+
const {protocol, hostname, port, pathname, query} = this.props;
49+
const urlObj = {protocol, hostname, port, pathname, query};
50+
const url = makeUrl(urlObj);
51+
scriptjs(url, () => this.setState({ isLoaded: true }));
52+
}
53+
54+
componentWillReceiveProps (nextProps) {
55+
if ("production" !== process.env.NODE_ENV) {
56+
const changedKeys = getUrlObjChangedKeys(this.props, nextProps);
57+
58+
warning(0 === changedKeys.length, `ScriptjsLoader doesn't support mutating url related props after initial render. Changed props: %s`, `[${ changedKeys.join(", ") }]`);
59+
}
60+
}
61+
62+
render () {
63+
if (this.state.isLoaded) {
64+
return this.props.googleMapElement;
65+
} else {
66+
return this.props.loadingElement;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)