Skip to content

Commit e59af7c

Browse files
author
Son Nguyen
authored
docs: use correct react router v5 api in example (#1111)
* docs: use correct react router v5 api in example * docs: add App demonstration in react router v5 example * docs: make app.js code consistent between examples * docs: note to use history v4 with react router v5 * docs: remove unneeded line in react router v5 example
1 parent f860c80 commit e59af7c

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

docs/example-react-router.mdx

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,43 @@ test('rendering a component that uses useLocation', () => {
133133

134134
## Testing Library and React Router v5
135135

136-
In React Router v5, you need to pass the history object as a whole to the Route
137-
component.
136+
```jsx
137+
// app.js
138+
import React from 'react'
139+
import {Link, Route, Switch, useLocation} from 'react-router-dom'
140+
141+
const About = () => <div>You are on the about page</div>
142+
const Home = () => <div>You are home</div>
143+
const NoMatch = () => <div>No match</div>
144+
145+
export const LocationDisplay = () => {
146+
const location = useLocation()
147+
148+
return <div data-testid="location-display">{location.pathname}</div>
149+
}
150+
151+
export const App = () => (
152+
<div>
153+
<Link to="/">Home</Link>
154+
155+
<Link to="/about">About</Link>
156+
157+
<Switch>
158+
<Route exact path="/" component={Home} />
159+
160+
<Route path="/about" component={About} />
161+
162+
<Route component={NoMatch} />
163+
</Switch>
164+
165+
<LocationDisplay />
166+
</div>
167+
)
168+
```
169+
170+
In your tests, pass the history object as a whole to the Router component.
171+
**Note:** React Router v5 [only works with History v4](https://github.com/remix-run/history#documentation),
172+
so make sure you have the correct version of `history` installed.
138173

139174
```jsx
140175
// app.test.js
@@ -151,7 +186,7 @@ import {App} from './app'
151186
test('full app rendering/navigating', async () => {
152187
const history = createMemoryHistory()
153188
render(
154-
<Router location={history.location} navigator={history}>
189+
<Router history={history}>
155190
<App />
156191
</Router>,
157192
)
@@ -170,7 +205,7 @@ test('landing on a bad page', () => {
170205
const history = createMemoryHistory()
171206
history.push('/some/bad/route')
172207
render(
173-
<Router location={history.location} navigator={history}>
208+
<Router history={history}>
174209
<App />
175210
</Router>,
176211
)

0 commit comments

Comments
 (0)