Skip to content

Commit 1d9cc93

Browse files
committed
Merge pull request #154 from taion/add-route-element
Pass in the base route element to render
2 parents c0780a9 + e991941 commit 1d9cc93

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,17 @@ This has the same signature as the `render` callback on `Relay.Renderer`, except
225225

226226
While transitioning, the ready state properties (`done`, `error`, `retry`, `stale`) will reflect the ready state of the transition as a whole. However, the `props` object in the `render` callback for a route will be populated as long as the data for that particular route are ready. For example, if a transition does not change the params to the queries for a parent route, the `render` callback for that route will have `props`, even while the `render` callbacks for its child routes may not.
227227

228-
The argument object to the render callback also includes an additional `routerProps` property, which contains the props from the router. Unlike `props`, `routerProps` will be populated regardless of the Relay ready state. This can be used to render child routes while the data for the parent route are still loading, or to otherwise use information from the router to control rendering before the Relay data are available.
228+
The argument object to the render callback includes two extra properties: `routerProps` and `element`. `routerProps` contains the props from the router, and unlike `props`, will be populated regardless of the Relay ready state. `routerProps` can be used to render child routes while the data for the parent route are still loading, or to otherwise use information from the router to control rendering before the Relay data are available. `element` contains the base route element without the props from Relay, and can be used to render the route component when the route uses a dynamic component, or when using other router middlewares that may wrap the route component:
229+
230+
```js
231+
({ props, routerProps, element }) => {
232+
if (!props) {
233+
return <Loading {...routerProps} />;
234+
}
235+
236+
return React.cloneElement(element, props);
237+
}
238+
```
229239

230240
When using named components, you can define these on a per-component basis, optionally omitting the callback for components that do not need a custom render callback:
231241

src/RouteContainer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function RouteContainer(
5454
...routerProps,
5555
...extraProps,
5656
},
57+
element: children,
5758
});
5859
} else if (props) {
5960
// The child already has routerProps, so just inject the additional props.

test/useRelay.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ describe('useRelay', () => {
187187
it('should have router props', () => {
188188
expect(renderArgs.routerProps).to.exist;
189189
expect(renderArgs.routerProps.route).to.exist;
190+
expect(renderArgs.element).to.exist;
191+
expect(renderArgs.element.type).to.equal(WidgetParentContainer);
190192
});
191193

192194
it('should support injected props', () => {
@@ -214,6 +216,8 @@ describe('useRelay', () => {
214216
expect(renderArgs.props.route).to.exist;
215217
expect(renderArgs.routerProps).to.exist;
216218
expect(renderArgs.routerProps.route).to.exist;
219+
expect(renderArgs.element).to.exist;
220+
expect(renderArgs.element.type).to.equal(WidgetParentContainer);
217221
});
218222

219223
it('should support injected props', () => {

0 commit comments

Comments
 (0)