Skip to content

Commit 08c75b5

Browse files
author
Ari
committed
WIP: updates to react-router-dom
1 parent 0aa2591 commit 08c75b5

File tree

4 files changed

+138
-109
lines changed

4 files changed

+138
-109
lines changed

examples/Container.js

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { Component } from 'react';
1+
import React, {Component} from 'react';
22

33
import GitHubForkRibbon from 'react-github-fork-ribbon';
44
import PropTypes from 'prop-types';
5-
import { Link } from 'react-router';
5+
import {withRouter, Switch, Link, Redirect, Route} from 'react-router-dom';
66

77
import styles from './styles.module.css';
88

@@ -11,47 +11,31 @@ const GoogleApiWrapper = __IS_DEV__
1111
: require('../dist').GoogleApiWrapper;
1212

1313
class Container extends Component {
14-
static propTypes = {
15-
children: PropTypes.element.isRequired
16-
};
14+
static propTypes = {};
1715

1816
static contextTypes = {
1917
router: PropTypes.object
2018
};
2119

22-
renderChildren = () => {
23-
const { children } = this.props;
24-
25-
if (!children) return;
26-
27-
const sharedProps = {
28-
google: this.props.google,
29-
loaded: this.props.loaded
30-
};
31-
32-
return React.Children.map(children, child =>
33-
React.cloneElement(child, sharedProps, {})
34-
);
35-
};
36-
3720
render() {
38-
const { routeMap, routeDef } = this.props;
21+
const {children, routes, routeDef} = this.props;
3922

4023
return (
4124
<div className={styles.container}>
4225
<GitHubForkRibbon
4326
href="//github.com/fullstackreact/google-maps-react"
4427
position="right"
45-
target="_blank">
28+
target="_blank"
29+
>
4630
Fork me on GitHub
4731
</GitHubForkRibbon>
4832

4933
<div className={styles.wrapper}>
5034
<div className={styles.list}>
5135
<ul>
52-
{Object.keys(routeMap).map(key => (
53-
<Link activeClassName={styles.active} key={key} to={key}>
54-
<li>{routeMap[key].name}</li>
36+
{routes.map(route => (
37+
<Link key={route.path} to={route.path}>
38+
<li>{route.name}</li>
5539
</Link>
5640
))}
5741
</ul>
@@ -68,7 +52,26 @@ class Container extends Component {
6852
</h2>
6953
</div>
7054

71-
{this.renderChildren()}
55+
<Switch>
56+
{routes.map(route => (
57+
<Route
58+
key={route.name}
59+
path={route.path}
60+
routeDef={route}
61+
routes={routes}
62+
render={routingProps => (
63+
<div>
64+
<route.component
65+
{...routingProps}
66+
google={this.props.google}
67+
loaded={this.props.loaded}
68+
/>
69+
</div>
70+
)}
71+
/>
72+
))}
73+
<Redirect path="*" to={'/basic'} />
74+
</Switch>
7275
</div>
7376
</div>
7477
</div>
@@ -78,8 +81,10 @@ class Container extends Component {
7881

7982
const Loading = () => <div>Fancy loading container</div>;
8083

81-
export default GoogleApiWrapper({
82-
apiKey: __GAPI_KEY__,
83-
libraries: ['places', 'visualization'],
84-
LoadingContainer: Loading
85-
})(Container);
84+
export default withRouter(
85+
GoogleApiWrapper({
86+
apiKey: __GAPI_KEY__,
87+
libraries: ['places', 'visualization'],
88+
LoadingContainer: Loading
89+
})(Container)
90+
);

examples/index.js

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33

44
import {
5-
hashHistory,
6-
IndexRoute,
7-
Link,
85
Redirect,
6+
Switch,
7+
Link,
98
Route,
10-
Router
11-
} from 'react-router';
9+
BrowserRouter as Router
10+
} from 'react-router-dom';
1211

1312
import Container from './Container';
1413

@@ -22,77 +21,84 @@ import Polygon from './components/withPolygons';
2221
import Polyline from './components/withPolylines';
2322
import CustomEvents from './components/resizeEvent';
2423

25-
const routeMap = {
26-
basic: {
24+
const routes = [
25+
{
26+
path: '/basic',
2727
name: 'Simple',
2828
component: Simple
2929
},
30-
markers: {
30+
{
31+
path: '/markers',
3132
name: 'Marker',
3233
component: Marker
3334
},
34-
clickable_markers: {
35+
{
36+
path: '/clickable_markers',
3537
name: 'Clickable markers',
3638
component: ClickableMarkers
3739
},
38-
places: {
40+
{
41+
path: '/places',
3942
name: 'Google places',
4043
component: GooglePlaces
4144
},
42-
autocomplete: {
45+
{
46+
path: '/autocomplete',
4347
name: 'Autocomplete',
4448
component: Autocomplete
4549
},
46-
heatMap: {
50+
{
51+
path: '/heatMap',
4752
name: 'Heat Map',
4853
component: HeatMap
4954
},
50-
polygons: {
55+
{
56+
path: '/polygons',
5157
name: 'Polygon',
5258
component: Polygon
5359
},
54-
polyline: {
60+
{
61+
path: '/polyline',
5562
name: 'Polyline',
5663
component: Polyline
5764
},
58-
onResizeEvent: {
65+
{
66+
path: '/onResizeEvent',
5967
name: 'Custom events',
6068
component: CustomEvents
6169
}
62-
};
70+
];
6371

64-
const createElement = (Component, props) => {
65-
const pathname = props.location.pathname.replace('/', '');
66-
const routeDef = routeMap[pathname];
72+
const createElement = (Component, route) => {
73+
// const pathname = props.location.pathname.replace('/', '');
74+
// const routeDef = routes[pathname];
6775

6876
const newProps = {
69-
routeMap,
70-
pathname,
71-
routeDef
77+
key: route.name,
78+
route,
79+
routes,
80+
// pathname,
81+
routeDef: route
82+
// routeDef
7283
};
7384

74-
return <Component {...newProps} {...props} />;
85+
return <Component {...newProps} />;
7586
};
7687

77-
const routes = (
78-
<Router createElement={createElement} history={hashHistory}>
79-
<Route component={Container} path="/">
80-
{Object.keys(routeMap).map(key => {
81-
const r = routeMap[key];
82-
83-
return (
84-
<Route key={key} path={key} name={r.name} component={r.component} />
85-
);
86-
})}
87-
88-
<IndexRoute component={routeMap.basic.component} />
89-
</Route>
88+
const Routing = (
89+
<Router>
90+
<Container routes={routes} />
9091
</Router>
9192
);
9293

94+
// <Route render={routeProps => createElement(Container, routeProps)} path="/">
95+
// {Object.keys(routes).map(key => {
96+
// const r = routes[key];
97+
// })}
98+
// </Route>
9399
const mountNode = document.querySelector('#root');
94100

95-
if (mountNode) ReactDOM.render(routes, mountNode);
101+
if (mountNode) ReactDOM.render(Routing, mountNode);
96102
else {
97103
const hljs = require('highlight.js');
98104

package-lock.json

Lines changed: 58 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)