Skip to content

Commit 3f5bfc9

Browse files
committed
make createRouterHistory composable
particularly to pass along options to useBaseName and useQueries
1 parent 93bf4b2 commit 3f5bfc9

File tree

7 files changed

+57
-12
lines changed

7 files changed

+57
-12
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import assert from 'assert'
2+
import expect from 'expect'
3+
import useRouterHistory from '../useRouterHistory'
4+
import createHistory from 'history/lib/createMemoryHistory'
5+
6+
describe('useRouterHistory', function () {
7+
it('adds backwards compatibility flag', function () {
8+
const history = useRouterHistory(createHistory)()
9+
expect(history.__v2_compatible__).toBe(true)
10+
})
11+
12+
it('passes along options, especially query parsing', function (done) {
13+
const history = useRouterHistory(createHistory)({
14+
stringifyQuery() {
15+
assert(true)
16+
done()
17+
}
18+
})
19+
20+
history.push({ pathname: '/', query: { test: true } })
21+
})
22+
})
23+

modules/browserHistory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import createRouterHistory from './createRouterHistory'
1+
import useRouterHistory from './useRouterHistory'
22
import createBrowserHistory from 'history/lib/createBrowserHistory'
33

4-
export default createRouterHistory(createBrowserHistory)
4+
export default useRouterHistory(createBrowserHistory)()

modules/createRouterHistory.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

modules/hashHistory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import createRouterHistory from './createRouterHistory'
1+
import useRouterHistory from './useRouterHistory'
22
import createHashHistory from 'history/lib/createHashHistory'
33

4-
export default createRouterHistory(createHashHistory)
4+
export default useRouterHistory(createHashHistory)()
55

modules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export { createRoutes } from './RouteUtils'
2020
export RouterContext from './RouterContext'
2121
export PropTypes from './PropTypes'
2222
export match from './match'
23+
export useRouterHistory from './useRouterHistory'
2324

2425
/* histories */
2526
export browserHistory from './browserHistory'

modules/useRouterHistory.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import useQueries from 'history/lib/useQueries'
2+
import useBasename from 'history/lib/useBasename'
3+
4+
export default function useRouterHistory(createHistory) {
5+
return function (options) {
6+
const history = useBasename(useQueries(createHistory))(options)
7+
history.__v2_compatible__ = true
8+
return history
9+
}
10+
}

upgrade-guides/v2.0.0.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,22 @@ const RouteComponent = React.createClass({
222222
})
223223
```
224224
225+
## Custom Query String Parsing
226+
227+
```js
228+
// v1.x
229+
<Router
230+
parseQueryString={parse}
231+
stringifyQueryString={stringify}
232+
/>
233+
234+
// v2.0.0
235+
import { useRouterHistory } from 'react-router'
236+
237+
const appHistory = useRouterHistory({
238+
parseQueryString: parse,
239+
stringifyQueryString: stringify
240+
})
241+
242+
<Router history={appHistory}/>
243+

0 commit comments

Comments
 (0)