You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+12-1Lines changed: 12 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
Thanks for contributing, you rock!
2
2
3
-
If you use our code, it is now *our* code.
3
+
If you use our code, it is now _our_ code.
4
4
5
5
Please read https://reactjs.org/ and the Code of Conduct before opening an issue.
6
6
@@ -12,21 +12,25 @@ Please read https://reactjs.org/ and the Code of Conduct before opening an issue
12
12
-[Development](#development)
13
13
14
14
<aname="bug"/></a>
15
+
15
16
## Think You Found a Bug?
16
17
17
18
Please provide a test case of some sort. Best is a pull request with a failing test. Next is a link to CodePen/JS Bin or repository that illustrates the bug. Finally, some copy/pastable code is acceptable.
18
19
19
20
<aname="api"/></a>
21
+
20
22
## Proposing New or Changed API?
21
23
22
24
Please provide thoughtful comments and some sample code. Proposals without substance will be closed.
23
25
24
26
<aname="attention"/></a>
27
+
25
28
## Issue Not Getting Attention?
26
29
27
30
If you need a bug fixed and nobody is fixing it, it is your responsibility to fix it. Issues with no activity for 30 days may be closed.
28
31
29
32
<aname="pr"/></a>
33
+
30
34
## Making a Pull Request?
31
35
32
36
Pull requests need only the :+1: of two or more collaborators to be merged; when the PR author is a collaborator, that counts as one.
@@ -51,12 +55,15 @@ The following steps will get you setup to contribute changes to this repo:
51
55
52
56
1. Fork the repo (click the <kbd>Fork</kbd> button at the top right of this page).
53
57
2. Clone your fork locally.
58
+
54
59
```bash
55
60
# in a terminal, cd to parent directory where you want your clone to be, then
3. Install dependencies and build. React Router uses `npm`, so you should too. If you install using `yarn`, unnecessary yarn lock files will be generated.
66
+
60
67
```bash
61
68
npm install
62
69
npm run build
@@ -73,6 +80,7 @@ React Router uses Lerna to manage the monorepo. Lerna sets up symlinks between t
73
80
### Building
74
81
75
82
Calling `npm run build` from the root directory will build every package. If you want to build a specific package, you should `cd` into that directory.
83
+
76
84
```bash
77
85
# build everything
78
86
npm run build
@@ -84,18 +92,21 @@ npm run build
84
92
### Testing
85
93
86
94
Calling `npm test` from the root directory will run **every** package's tests. If you want to run tests for a specific package, you should `cd` into that directory.
95
+
87
96
```bash
88
97
# all tests
89
98
npm test
90
99
# react-router-dom tests
91
100
cd packages/react-router-dom
92
101
npm test
93
102
```
103
+
94
104
React Router uses Jest to run its tests, so you can provide the `--watch` flag to automatically re-run tests when files change.
95
105
96
106
### Website
97
107
98
108
The code for the documentation website lives in the `website` directory. `cd` into there and call `npm start` to start a webpack dev server on `localhost:8080` that will watch for changes.
Copy file name to clipboardExpand all lines: FAQ.md
+33-27Lines changed: 33 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Frequently Asked Questions
2
2
3
-
This is a list of support questions that frequently show up in GitHub issues. This list is intended to minimize the frequency of this happening. The issues section is intended for bug reports, not developer support. Support questions should be asked at StackOverflow or in the Reactiflux chat.
3
+
This is a list of support questions that frequently show up in GitHub issues. This list is intended to minimize the frequency of this happening. The issues section is intended for bug reports, not developer support. Support questions should be asked at StackOverflow or in the Reactiflux chat.
4
4
5
5
If there is a support question that you frequently see being asked, please open a PR to add it to this list.
6
6
7
-
*[Why aren't my components updating when the location changes?](#why-arent-my-components-updating-when-the-location-changes)
8
-
*[Why doesn't my application render after refreshing?](#why-doesnt-my-application-render-after-refreshing)
9
-
*[Why doesn't my application work when loading nested routes?](#why-doesnt-my-application-work-when-loading-nested-routes)
10
-
*[How do I access the `history` object outside of components?](#how-do-i-access-the-history-object-outside-of-components)
11
-
*[How do I pass props to the component rendered by a `<Route>`?](#how-do-i-pass-props-to-the-component-rendered-by-a-route)
7
+
-[Why aren't my components updating when the location changes?](#why-arent-my-components-updating-when-the-location-changes)
8
+
-[Why doesn't my application render after refreshing?](#why-doesnt-my-application-render-after-refreshing)
9
+
-[Why doesn't my application work when loading nested routes?](#why-doesnt-my-application-work-when-loading-nested-routes)
10
+
-[How do I access the `history` object outside of components?](#how-do-i-access-the-history-object-outside-of-components)
11
+
-[How do I pass props to the component rendered by a `<Route>`?](#how-do-i-pass-props-to-the-component-rendered-by-a-route)
12
12
13
13
### Why aren't my components updating when the location changes?
14
14
@@ -19,13 +19,14 @@ React Router relies on updates propagating from your router component to every c
19
19
If your application is hosted on a static file server, you need to use a `<HashRouter>` instead of a `<BrowserRouter>`.
20
20
21
21
```js
22
-
import { HashRouter } from'react-router-dom'
22
+
import { HashRouter } from"react-router-dom";
23
23
24
-
ReactDOM.render((
24
+
ReactDOM.render(
25
25
<HashRouter>
26
26
<App />
27
-
</HashRouter>
28
-
), holder)
27
+
</HashRouter>,
28
+
holder
29
+
);
29
30
```
30
31
31
32
When you load the root page of a website hosted on a static file server (e.g., `http://www.example.com`), a `<BrowserHistory>` might appear to work. However, this is only because when the browser makes the request for the root page, the server responds with the root `index.html` file.
@@ -45,9 +46,9 @@ However, you will end up with a blank screen if you were to refresh a non-root p
45
46
This is not an issue when your server can respond to dynamic requests. In that situation, you can instruct the server to catch all requests and serve up the same `index.html` file.
When you use a static server, your application should have just one `index.html` file.
@@ -75,6 +76,7 @@ If the `src` of the `<script>` tag that is used to load your application has a r
75
76
<scriptsrc='static/js/bundle.js'></script>
76
77
<scriptsrc='./static/js/bundle.js'></script>
77
78
```
79
+
78
80
### How do I access the `history` object outside of components?
79
81
80
82
When you use the `<BrowserRouter>`, `<HashRouter>`, `<MemoryRouter>`, and `<NativeRouter>`, a `history` object will be created for you. This is convenient, and the `history` object is readily accessible from within your React components, but it can be a pain to use it outside of them. If you need to access a `history` object outside of your components, you will need to create your own `history` object (in its own module) and import it throughout your project.
@@ -83,23 +85,26 @@ If you do this, make sure that you use the generic `<Router>` component and not
83
85
84
86
```js
85
87
// history.js
86
-
import { createBrowserHistory } from'history'
87
-
exportdefaultcreateBrowserHistory()
88
+
import { createBrowserHistory } from"history";
89
+
exportdefaultcreateBrowserHistory();
88
90
```
91
+
89
92
```js
90
93
// index.js
91
-
import { Router } from'react-router-dom';
92
-
importhistoryfrom'./history'
94
+
import { Router } from"react-router-dom";
95
+
importhistoryfrom"./history";
93
96
94
-
ReactDOM.render((
97
+
ReactDOM.render(
95
98
<Router history={history}>
96
99
<App />
97
-
</Router>
98
-
), document.getElementById('root'))
100
+
</Router>,
101
+
document.getElementById("root")
102
+
);
99
103
```
104
+
100
105
```js
101
106
// nav.js
102
-
importhistoryfrom'./history'
107
+
importhistoryfrom"./history";
103
108
104
109
exportdefaultfunctionnav(loc) {
105
110
history.push(loc);
@@ -116,11 +121,12 @@ If you need to pass props to the component rendered by a `<Route>`, you should u
var matchRoutes =require('react-router-config').matchRoutes
23
+
var matchRoutes =require("react-router-config").matchRoutes;
24
24
```
25
25
26
26
The UMD build is also available on [unpkg](https://unpkg.com):
@@ -52,23 +52,27 @@ Routes are objects with the same properties as a `<Route>` with a couple differe
52
52
53
53
```js
54
54
constroutes= [
55
-
{ component: Root,
55
+
{
56
+
component: Root,
56
57
routes: [
57
-
{ path:'/',
58
+
{
59
+
path:"/",
58
60
exact:true,
59
61
component: Home
60
62
},
61
-
{ path:'/child/:id',
63
+
{
64
+
path:"/child/:id",
62
65
component: Child,
63
66
routes: [
64
-
{ path:'/child/:id/grand-child',
67
+
{
68
+
path:"/child/:id/grand-child",
65
69
component: GrandChild
66
70
}
67
71
]
68
72
}
69
73
]
70
74
}
71
-
]
75
+
];
72
76
```
73
77
74
78
**Note**: Just like `<Route>`, relative paths are not (yet) supported. When it is supported there, it will be supported here.
@@ -80,12 +84,13 @@ const routes = [
80
84
Returns an array of matched routes.
81
85
82
86
#### Parameters
87
+
83
88
- routes - the route configuration
84
89
- pathname - the [pathname](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname) component of the url. This must be a decoded string representing the path.
85
90
86
91
```js
87
-
import { matchRoutes } from'react-router-config'
88
-
constbranch=matchRoutes(routes, '/child/23')
92
+
import { matchRoutes } from"react-router-config";
93
+
constbranch=matchRoutes(routes, "/child/23");
89
94
// using the routes shown earlier, this returns
90
95
// [
91
96
// routes[0],
@@ -99,8 +104,8 @@ Each item in the array contains two properties: `routes` and `match`.
99
104
-`match`: The match object that also gets passed to `<Route>` render methods.
100
105
101
106
```js
102
-
branch[0].match.url
103
-
branch[0].match.isExact
107
+
branch[0].match.url;
108
+
branch[0].match.isExact;
104
109
// etc.
105
110
```
106
111
@@ -191,63 +196,65 @@ Again, that's all pseudo-code. There are a lot of ways to do server rendering wi
191
196
In order to ensure that matching outside of render with `matchRoutes` and inside of render result in the same branch, you must use `renderRoutes` instead of `<Route>` inside your components. You can render a `<Route>` still, but know that it will not be accounted for in `matchRoutes` outside of render.
0 commit comments