Skip to content

Commit 4f5b6a8

Browse files
committed
2.9.3
2.9.3 - 2017-10-18 ----------------------------------------------- ## Routing * Adds app-wide `history` instance to `kit/lib/routing.js`, for controlling routes outside of React directly * Replaces React Router's `<BrowserRouter>` with `<Router history={history}>`, to stay in sync with global pushstate
1 parent 855ee3e commit 4f5b6a8

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2.9.3 - 2017-10-18
2+
-----------------------------------------------
3+
4+
## Routing
5+
* Adds app-wide `history` instance to `kit/lib/routing.js`, for controlling routes outside of React directly
6+
* Replaces React Router's `<BrowserRouter>` with `<Router history={history}>`, to stay in sync with global pushstate
7+
18
2.9.2 - 2017-10-17
29
-----------------------------------------------
310

kit/entry/browser.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import 'isomorphic-fetch';
1616
import React from 'react';
1717
import ReactDOM from 'react-dom';
1818

19-
// Browser routing
20-
import { BrowserRouter } from 'react-router-dom';
19+
// React Router, for controlling browser routes. We'll feed in our custom
20+
// `history` instance that's imported below, so that we have a single store of
21+
// truth for routing
22+
import { Router } from 'react-router-dom';
2123

2224
// Apollo Provider. This HOC will 'wrap' our React component chain
2325
// and handle injecting data down to any listening component
@@ -31,6 +33,9 @@ import { ApolloProvider } from 'react-apollo';
3133
// first, since it sets up our app's settings
3234
import App from 'src/app';
3335

36+
// Get the custom `history` that we'll use to feed down to our `<Router>`
37+
import { history } from 'kit/lib/routing';
38+
3439
// Grab the shared Apollo Client
3540
import { browserClient } from 'kit/lib/apollo';
3641

@@ -68,9 +73,9 @@ const Root = (() => {
6873
// can respond to route changes
6974
const Chain = () => (
7075
<ApolloProvider store={store} client={client}>
71-
<BrowserRouter>
76+
<Router history={history}>
7277
<App />
73-
</BrowserRouter>
78+
</Router>
7479
</ApolloProvider>
7580
);
7681

kit/lib/routing.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
// ----------------------
44
// IMPORTS
55

6+
/* NPM */
67
import React from 'react';
78
import PropTypes from 'prop-types';
89

10+
// Browser history, that we can use to control URL pushstate throughout our
11+
// entire app
12+
import createBrowserHistory from 'history/createBrowserHistory';
13+
14+
// React Router
915
import { Route, Redirect as ReactRouterRedirect } from 'react-router-dom';
1016

1117
// ----------------------
1218

19+
// Create and export a custom history
20+
export const history = !SERVER && createBrowserHistory();
21+
1322
// <Status code="xxx"> component. Updates the context router's context, which
1423
// can be used by the server handler to respond to the status on the server.
1524
class Status extends React.PureComponent {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "reactql-app",
3-
"version": "2.9.2",
3+
"version": "2.9.3",
44
"description": "ReactQL starter kit app",
55
"license": "UNLICENSED",
66
"private": true,
@@ -87,6 +87,7 @@
8787
"boxen": "^1.2.1",
8888
"chalk": "^2.1.0",
8989
"graphql": "^0.11.7",
90+
"history": "^4.7.2",
9091
"ip": "^1.1.5",
9192
"isomorphic-fetch": "^2.2.1",
9293
"kcors": "^2.2.1",

src/components/main/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ import Helmet from 'react-helmet';
4040

4141
/* ReactQL */
4242

43-
// NotFound 404 handler for unknown routes
44-
import { Redirect } from 'kit/lib/routing';
43+
// NotFound 404 handler for unknown routes, and the app-wide `history` object
44+
// we can use to make route changes from anywhere
45+
import { Redirect, history } from 'kit/lib/routing';
4546

4647
/* App */
4748

@@ -63,6 +64,13 @@ import logo from './reactql-logo.svg';
6364

6465
// ----------------------
6566

67+
// Example function to show that the `history` object can be changed from
68+
// anywhere, simply by importing it-- use this in Redux actions, functions,
69+
// React `onClick` events, etc.
70+
function changeRoute() {
71+
history.push('/page/about');
72+
}
73+
6674
export default () => (
6775
<div>
6876
<Helmet>
@@ -82,6 +90,7 @@ export default () => (
8290
<li><Link to="/page/contact">Contact</Link></li>
8391
<li><Link to="/old/path">Redirect from /old/path &#8594; /new/path</Link></li>
8492
</ul>
93+
Change routes anywhere &mdash; <button onClick={changeRoute}>Like here (About)</button>
8594
<hr />
8695
<Switch>
8796
<Route exact path="/" component={Home} />

0 commit comments

Comments
 (0)