Skip to content

Commit a309811

Browse files
committed
Tweak extra props middleware documentation
1 parent 34ba72d commit a309811

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

docs/Troubleshooting.md

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,47 @@ You should generally attempt to use this pattern as sparingly as possible. In ge
8888

8989
### Passing additional values into route components
9090

91-
There are multiple ways to do this depending on what you want to do. You can:
92-
93-
- Define additional values on `<Route>` or the plain route. This will make those values available on `this.props.route` on route components.
94-
- Pass in a `createElement` handler to `<Router>` or `<RouterContext>`. This will allow you to inject additional props into route elements at creation time.
95-
- Pass in a `render` handler to `<Router>` with the result of `applyRouterMiddleware`, using a middleware such as:
96-
```javascript
97-
extraProps => ({
98-
renderRouteComponent: (child) => React.cloneElement(child, extraProps)
99-
})
91+
There are multiple ways to do this depending on what you want specifically.
92+
93+
#### Declare properties on the route
94+
95+
You can define additional props on `<Route>` or on the plain route:
96+
97+
```js
98+
<Route foo="bar" />
99+
```
100+
101+
These properties will then be available on `this.props.route` on the route component, such as with `this.props.route.foo` above.
102+
103+
#### Inject props to all routes via middleware
104+
105+
You can define a middleware that injects additional props into each route component:
106+
107+
```js
108+
const useExtraProps = {
109+
renderRouteComponent: child => React.cloneElement(child, extraProps)
110+
}
111+
```
112+
113+
You can then use this middleware with:
114+
115+
```js
116+
<Router
117+
history={history}
118+
routes={routes}
119+
render={applyRouterMiddleware(useExtraProps)}
120+
/>
121+
```
122+
123+
#### Use a top-level context provider
124+
125+
You can export React context on a top-level provider component, then access this data throughout the tree on rendered components.
126+
127+
```js
128+
<ExtraDataProvider>
129+
<Router history={history} routes={routes} />
130+
</ExtraDataProvider>
100131
```
101-
- Define a top-level component above `<Router>` or `<RouterContext>` that exports additional values via `getChildContext`, then access them via context from rendered components.
102132

103133

104134
### `<noscript>` with server-side rendering and async routes

0 commit comments

Comments
 (0)