Skip to content

Commit 78171da

Browse files
authored
Update examples and docs to use hooks API (remix-run#6976)
1 parent 25d0ba7 commit 78171da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1045
-774
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
printWidth: 70

packages/react-router-dom/docs/api/BrowserRouter.md

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
A [`<Router>`](../../../react-router/docs/api/Router.md) that uses the HTML5 history API (`pushState`, `replaceState` and the `popstate` event) to keep your UI in sync with the URL.
44

55
```jsx
6-
import { BrowserRouter } from 'react-router-dom'
7-
86
<BrowserRouter
97
basename={optionalString}
108
forceRefresh={optionalBool}
119
getUserConfirmation={optionalFunc}
1210
keyLength={optionalNumber}
1311
>
14-
<App/>
12+
<App />
1513
</BrowserRouter>
1614
```
1715

@@ -29,22 +27,21 @@ The base URL for all locations. If your app is served from a sub-directory on yo
2927
A function to use to confirm navigation. Defaults to using [`window.confirm`](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm).
3028

3129
```jsx
32-
// this is the default behavior
33-
function getConfirmation(message, callback) {
34-
const allowTransition = window.confirm(message);
35-
callback(allowTransition);
36-
}
37-
38-
<BrowserRouter getUserConfirmation={getConfirmation} />;
30+
<BrowserRouter
31+
getUserConfirmation={(message, callback) => {
32+
// this is the default behavior
33+
const allowTransition = window.confirm(message);
34+
callback(allowTransition);
35+
}}
36+
/>
3937
```
4038

4139
## forceRefresh: bool
4240

43-
If `true` the router will use full page refreshes on page navigation. You probably only want this in [browsers that don't support the HTML5 history API](http://caniuse.com/#feat=history).
41+
If `true` the router will use full page refreshes on page navigation. You may want to use this to imitate the way a traditional server-rendered app would work with full page refreshes between page navigation.
4442

4543
```jsx
46-
const supportsHistory = 'pushState' in window.history
47-
<BrowserRouter forceRefresh={!supportsHistory} />
44+
<BrowserRouter forceRefresh={true} />
4845
```
4946

5047
## keyLength: number
@@ -57,4 +54,6 @@ The length of `location.key`. Defaults to 6.
5754

5855
## children: node
5956

60-
A [single child element](https://facebook.github.io/react/docs/react-api.html#reactchildrenonly) to render.
57+
The child elements to render.
58+
59+
Note: On React &lt; 16 you must use a [single child element](https://facebook.github.io/react/docs/react-api.html#reactchildrenonly) since a render method cannot return more than one element. If you need more than one element, you might try wrapping them in an extra `<div>`.

packages/react-router-dom/docs/api/HashRouter.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ A [`<Router>`](../../../react-router/docs/api/Router.md) that uses the hash port
55
**IMPORTANT NOTE:** Hash history does not support `location.key` or `location.state`. In previous versions we attempted to shim the behavior but there were edge-cases we couldn't solve. Any code or plugin that needs this behavior won't work. As this technique is only intended to support legacy browsers, we encourage you to configure your server to work with `<BrowserHistory>` instead.
66

77
```jsx
8-
import { HashRouter } from 'react-router-dom'
9-
10-
<HashRouter>
11-
<App/>
8+
<HashRouter
9+
basename={optionalString}
10+
getUserConfirmation={optionalFunc}
11+
hashType={optionalString}
12+
>
13+
<App />
1214
</HashRouter>
1315
```
1416

@@ -26,13 +28,13 @@ The base URL for all locations. A properly formatted basename should have a lead
2628
A function to use to confirm navigation. Defaults to using [`window.confirm`](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm).
2729

2830
```jsx
29-
// this is the default behavior
30-
function getConfirmation(message, callback) {
31-
const allowTransition = window.confirm(message);
32-
callback(allowTransition);
33-
}
34-
35-
<HashRouter getUserConfirmation={getConfirmation} />;
31+
<HashRouter
32+
getUserConfirmation={(message, callback) => {
33+
// this is the default behavior
34+
const allowTransition = window.confirm(message);
35+
callback(allowTransition);
36+
}}
37+
/>
3638
```
3739

3840
## hashType: string

packages/react-router-dom/docs/api/Link.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
Provides declarative, accessible navigation around your application.
44

55
```jsx
6-
import { Link } from 'react-router-dom'
7-
86
<Link to="/about">About</Link>
97
```
108

@@ -58,22 +56,28 @@ When `true`, clicking the link will replace the current entry in the history sta
5856

5957
## innerRef: function
6058

61-
Allows access to the underlying `ref` of the component
59+
As of React Router 5.1, if you are using React 16 you should not need this prop because we [forward the ref](https://reactjs.org/docs/forwarding-refs.html) to the underlying `<a>`. Use a normal `ref` instead.
6260

63-
```jsx
64-
const refCallback = node => {
65-
// `node` refers to the mounted DOM element or null when unmounted
66-
}
61+
Allows access to the underlying `ref` of the component.
6762

68-
<Link to="/" innerRef={refCallback} />
63+
```jsx
64+
<Link
65+
to="/"
66+
innerRef={node => {
67+
// `node` refers to the mounted DOM element
68+
// or null when unmounted
69+
}}
70+
/>
6971
```
7072

7173
## innerRef: RefObject
7274

73-
Get the underlying `ref` of the component with `React.createRef()`
75+
As of React Router 5.1, if you are using React 16 you should not need this prop because we [forward the ref](https://reactjs.org/docs/forwarding-refs.html) to the underlying `<a>`. Use a normal `ref` instead.
76+
77+
Get the underlying `ref` of the component using [`React.createRef`](https://reactjs.org/docs/refs-and-the-dom.html#creating-refs).
7478

7579
```jsx
76-
const anchorRef = React.createRef()
80+
let anchorRef = React.createRef()
7781

7882
<Link to="/" innerRef={anchorRef} />
7983
```

packages/react-router-dom/docs/api/NavLink.md

+12-13
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
A special version of the [`<Link>`](Link.md) that will add styling attributes to the rendered element when it matches the current URL.
44

55
```jsx
6-
import { NavLink } from 'react-router-dom'
7-
86
<NavLink to="/about">About</NavLink>
97
```
108

@@ -59,19 +57,20 @@ When `true`, the trailing slash on a location's `pathname` will be taken into co
5957
A function to add extra logic for determining whether the link is active. This should be used if you want to do more than verify that the link's pathname matches the current URL's `pathname`.
6058

6159
```jsx
62-
// only consider an event active if its event id is an odd number
63-
const oddEvent = (match, location) => {
64-
if (!match) {
65-
return false
66-
}
67-
const eventID = parseInt(match.params.eventID)
68-
return !isNaN(eventID) && eventID % 2 === 1
69-
}
70-
7160
<NavLink
7261
to="/events/123"
73-
isActive={oddEvent}
74-
>Event 123</NavLink>
62+
isActive={(match, location) => {
63+
if (!match) {
64+
return false;
65+
}
66+
67+
// only consider an event active if its event id is an odd number
68+
const eventID = parseInt(match.params.eventID);
69+
return !isNaN(eventID) && eventID % 2 === 1;
70+
}}
71+
>
72+
Event 123
73+
</NavLink>
7574
```
7675

7776
## location: object

packages/react-router-dom/docs/guides/code-splitting.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ One great feature of the web is that we don't have to make our visitors download
1414
[`loadable-components`] is a library for loading components with dynamic imports. It handles all sorts of edge cases automatically and makes code splitting simple! Here's an example of how to use [`loadable-components`]:
1515

1616
```jsx
17-
import loadable from '@loadable/component'
18-
import Loading from "./Loading";
17+
import loadable from "@loadable/component";
18+
import Loading from "./Loading.js";
1919

20-
const LoadableComponent = loadable(() => import('./Dashboard'), {
21-
fallback: <Loading />,
22-
})
20+
const LoadableComponent = loadable(() => import("./Dashboard.js"), {
21+
fallback: <Loading />
22+
});
2323

2424
export default class LoadableDashboard extends React.Component {
2525
render() {
@@ -37,7 +37,7 @@ Full documentation is available [here](https://www.smooth-code.com/open-source/l
3737
[`loadable-components`] includes [a guide for server-side rendering][ssr].
3838

3939
[babel]: https://babeljs.io/
40-
[`@babel/preset-react`]: https://babeljs.io/docs/en/babel-preset-react
40+
[`@babel/preset-react`]: https://babeljs.io/docs/en/babel-preset-react
4141
[`@babel/plugin-syntax-dynamic-import`]: https://babeljs.io/docs/plugins/syntax-dynamic-import/
4242
[`babel-plugin-import-inspector`]: https://github.com/thejameskyle/react-loadable/tree/6902cc87f618446c54daa85d8fecec6836c9461a#babel-plugin-import-inspector
4343
[`loadable-components`]: https://github.com/smooth-code/loadable-components

packages/react-router-dom/docs/guides/primary-components.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ If no `<Route>` matches, the `<Switch>` renders nothing (`null`).
4949
```jsx
5050
import React from "react";
5151
import ReactDOM from "react-dom";
52-
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
52+
import {
53+
BrowserRouter as Router,
54+
Switch,
55+
Route
56+
} from "react-router-dom";
5357

5458
function App() {
5559
return (

packages/react-router-dom/docs/guides/quick-start.md

+67-50
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,12 @@ Note: Behind the scenes a `<Link>` renders an `<a>` with a real `href`, so peopl
2828

2929
```jsx
3030
import React from "react";
31-
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
32-
33-
function Home() {
34-
return <h2>Home</h2>;
35-
}
36-
37-
function About() {
38-
return <h2>About</h2>;
39-
}
40-
41-
function Users() {
42-
return <h2>Users</h2>;
43-
}
31+
import {
32+
BrowserRouter as Router,
33+
Switch,
34+
Route,
35+
Link
36+
} from "react-router-dom";
4437

4538
export default function App() {
4639
return (
@@ -77,6 +70,18 @@ export default function App() {
7770
</Router>
7871
);
7972
}
73+
74+
function Home() {
75+
return <h2>Home</h2>;
76+
}
77+
78+
function About() {
79+
return <h2>About</h2>;
80+
}
81+
82+
function Users() {
83+
return <h2>Users</h2>;
84+
}
8085
```
8186

8287
## 2nd Example: Nested Routing
@@ -85,7 +90,46 @@ This example shows how nested routing works. The route `/topics` loads the `Topi
8590

8691
```jsx
8792
import React from "react";
88-
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
93+
import {
94+
BrowserRouter as Router,
95+
Switch,
96+
Route,
97+
Link,
98+
useRouterMatch,
99+
useParams
100+
} from "react-router-dom";
101+
102+
export default function App() {
103+
return (
104+
<Router>
105+
<div>
106+
<ul>
107+
<li>
108+
<Link to="/">Home</Link>
109+
</li>
110+
<li>
111+
<Link to="/about">About</Link>
112+
</li>
113+
<li>
114+
<Link to="/topics">Topics</Link>
115+
</li>
116+
</ul>
117+
118+
<Switch>
119+
<Route path="/about">
120+
<About />
121+
</Route>
122+
<Route path="/topics">
123+
<Topics />
124+
</Route>
125+
<Route path="/">
126+
<Home />
127+
</Route>
128+
</Switch>
129+
</div>
130+
</Router>
131+
);
132+
}
89133

90134
function Home() {
91135
return <h2>Home</h2>;
@@ -95,7 +139,9 @@ function About() {
95139
return <h2>About</h2>;
96140
}
97141

98-
function Topics({ match }) {
142+
function Topics() {
143+
let match = useRouterMatch();
144+
99145
return (
100146
<div>
101147
<h2>Topics</h2>
@@ -105,7 +151,9 @@ function Topics({ match }) {
105151
<Link to={`${match.url}/components`}>Components</Link>
106152
</li>
107153
<li>
108-
<Link to={`${match.url}/props-v-state`}>Props v. State</Link>
154+
<Link to={`${match.url}/props-v-state`}>
155+
Props v. State
156+
</Link>
109157
</li>
110158
</ul>
111159

@@ -125,40 +173,9 @@ function Topics({ match }) {
125173
);
126174
}
127175

128-
function Topic({ match }) {
129-
return <h3>Requested topic ID: {match.params.topicId}</h3>;
130-
}
131-
132-
function Navigation() {
133-
return (
134-
<ul>
135-
<li>
136-
<Link to="/">Home</Link>
137-
</li>
138-
<li>
139-
<Link to="/about">About</Link>
140-
</li>
141-
<li>
142-
<Link to="/topics">Topics</Link>
143-
</li>
144-
</ul>
145-
);
146-
}
147-
148-
export default function App() {
149-
return (
150-
<Router>
151-
<div>
152-
<Navigation />
153-
154-
<Switch>
155-
<Route path="/about" component={About} />
156-
<Route path="/topics" component={Topics} />
157-
<Route path="/" component={Home} />
158-
</Switch>
159-
</div>
160-
</Router>
161-
);
176+
function Topic() {
177+
let { topicId } = useParams();
178+
return <h3>Requested topic ID: {topicId}</h3>;
162179
}
163180
```
164181

0 commit comments

Comments
 (0)