Skip to content

Commit becc3af

Browse files
committed
Document server rendering API changes
1 parent 5dc7ea0 commit becc3af

4 files changed

Lines changed: 137 additions & 243 deletions

File tree

versioned_docs/version-8.x/configuring-links.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,6 @@ function NotFoundScreen({ route }) {
998998
}
999999
```
10001000
1001-
When doing server rendering, you'd also want to return correct status code for 404 errors. See [server rendering docs](server-rendering.md#handling-404-or-other-status-codes) for a guide on how to handle it.
1002-
10031001
## Rendering an initial route
10041002
10051003
Sometimes you want to ensure that a certain screen will always be present as the first screen in the navigator's state. You can use the `initialRouteName` property to specify the screen to use for the initial screen.

versioned_docs/version-8.x/server-container.md

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,61 @@ The `ServerContainer` component provides utilities to render your app on server
99
Example:
1010

1111
```js
12-
// Ref which will be populated with the screen options
13-
const ref = React.createRef();
12+
import { ServerContainer } from '@react-navigation/native/server';
13+
import { renderToPipeableStream } from 'react-dom/server';
1414

15-
// Location object - can be a URL object or an object with `pathname` and `search` fields
15+
// Location object for the incoming request
1616
const location = new URL('/profile?user=jane', 'https://example.org/');
1717

18-
// Get rendered HTML
19-
const html = ReactDOMServer.renderToString(
20-
<ServerContainer ref={ref} location={location}>
18+
const { pipe } = renderToPipeableStream(
19+
<ServerContainer location={location}>
2120
<App />
22-
</ServerContainer>
21+
</ServerContainer>,
22+
{
23+
onShellReady() {
24+
response.statusCode = 200;
25+
response.setHeader('Content-Type', 'text/html');
26+
pipe(response);
27+
},
28+
}
2329
);
24-
25-
// Then you can access the options for the current screen in the ref
26-
const options = ref.current?.getCurrentOptions(); // { title: 'My Profile' }
2730
```
2831

2932
The `ServerContainer` component should wrap your entire app during server rendering. Note that you still need a `NavigationContainer` in your app, `ServerContainer` doesn't replace it.
3033

3134
See the [`server rendering guide`](server-rendering.md) for a detailed guide and examples.
3235

33-
## Ref
34-
35-
If you attach a `ref` to the container, you can get the options for the current screen after rendering the app. The `ref` will contain a method called `getCurrentOptions` which will return an object with options for the focused screen in the navigation tree:
36-
37-
```js
38-
const options = ref.current.getCurrentOptions();
39-
```
40-
41-
Then you can access the options for the screen from this object and put it in the HTML:
42-
43-
```jsx
44-
<title>{options.title}</title>
45-
<meta name="description" content={options.description} />
46-
```
47-
48-
Note that the `options` object can be undefined if you are not rendering a navigator on the initial render.
49-
5036
## Props
5137

5238
### `location`
5339

54-
Location object containing the location to use for server rendered output. You can pass the `pathname` and `search` properties matching the `location` object in the browsers:
40+
`URL` object containing the location to use for server rendered output:
5541

5642
```js
57-
<ServerContainer location={{ pathname: '/profile', search: '' }}>
43+
<ServerContainer location={new URL('/profile', 'https://example.org/')}>
5844
<App />
5945
</ServerContainer>
6046
```
6147

6248
Normally, you'd construct this object based on the incoming request.
6349

64-
Basic example with Koa (don't use as is in production):
50+
Basic example with a Node server:
6551

6652
```js
67-
app.use(async (ctx) => {
68-
const html = ReactDOMServer.renderToString(
69-
<ServerContainer location={{ pathname: ctx.path, search: ctx.search }}>
53+
function handler(request, response) {
54+
const location = new URL(request.url, 'https://example.org/');
55+
56+
const { pipe } = renderToPipeableStream(
57+
<ServerContainer location={location}>
7058
<App />
71-
</ServerContainer>
59+
</ServerContainer>,
60+
{
61+
onShellReady() {
62+
response.statusCode = 200;
63+
response.setHeader('Content-Type', 'text/html');
64+
pipe(response);
65+
},
66+
}
7267
);
73-
74-
ctx.body = html;
75-
});
68+
}
7669
```

0 commit comments

Comments
 (0)