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: versioned_docs/version-5.x/configuring-links.md
+6-4
Original file line number
Diff line number
Diff line change
@@ -202,17 +202,17 @@ For example:
202
202
Settings:'settings',
203
203
},
204
204
},
205
-
404:'*',
205
+
NotFound:'*',
206
206
}
207
207
```
208
208
209
-
Here, we have defined a route named `404` and set it to match `*` aka everything. If the path didn't match `user/:id` or `settings`, it'll be matched by this route.
209
+
Here, we have defined a route named `NotFound` and set it to match `*` aka everything. If the path didn't match `user/:id` or `settings`, it'll be matched by this route.
210
210
211
211
So, a path like `/library` or `/settings/notification` will resolve to the following state object:
212
212
213
213
```js
214
214
conststate= {
215
-
routes: [{ name:'404' }],
215
+
routes: [{ name:'NotFound' }],
216
216
};
217
217
```
218
218
@@ -232,7 +232,7 @@ You can even go more specific, for example, say if you want to show a different
232
232
},
233
233
},
234
234
},
235
-
404:'*',
235
+
NotFound:'*',
236
236
}
237
237
```
238
238
@@ -260,6 +260,8 @@ const state = {
260
260
};
261
261
```
262
262
263
+
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.
264
+
263
265
## Rendering an initial route
264
266
265
267
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.
Copy file name to clipboardExpand all lines: versioned_docs/version-5.x/server-rendering.md
+57
Original file line number
Diff line number
Diff line change
@@ -144,6 +144,63 @@ Make sure that you have specified a `title` option in your screens:
144
144
/>
145
145
```
146
146
147
+
## Handling 404 or other status codes
148
+
149
+
When [rendering a screen for an invalid URL](configuring-links.md#handling-unmatched-routes-or-404), we should also return a `404` status code from the server.
150
+
151
+
First, we need to create a context where we'll attach the status code. To do this, place the following code in a separate file that we will be importing on both the server and client:
152
+
153
+
```js
154
+
import*asReactfrom'react';
155
+
156
+
constStatusCodeContext=React.createContext();
157
+
158
+
exportdefaultStatusCodeContext;
159
+
```
160
+
161
+
Then, we need to use the context in our `NotFound` screen. Here, we add a `code` property with the value of `404` to signal that the screen was not found:
162
+
163
+
```js
164
+
functionNotFound() {
165
+
conststatus=React.useContext(StatusCodeContext);
166
+
167
+
if (status) {
168
+
staus.code=404;
169
+
}
170
+
171
+
return (
172
+
<View>
173
+
<Text>Oops! This URL doesn't exist.</Text>
174
+
</View>
175
+
);
176
+
}
177
+
```
178
+
179
+
You could also attach additional information in this object if you need to.
180
+
181
+
Next, we need to create a status object to pass in the context on our server. By default, we'll set the `code` to `200`. Then pass the object in`StatusCodeContext.Provider` which should wrap the element with`ServerContainer`:
182
+
183
+
```js
184
+
// Create a status object
185
+
const status = { code: 200 };
186
+
187
+
const html = ReactDOMServer.renderToString(
188
+
// Pass the status object via context
189
+
<StatusCodeContext.Provider value={status}>
190
+
<ServerContainer ref={ref} location={location}>
191
+
{element}
192
+
</ServerContainer>
193
+
</StatusCodeContext.Provider>
194
+
);
195
+
196
+
// After rendering, get the status code and use it for server's response
197
+
ctx.status = status.code;
198
+
```
199
+
200
+
After we render the app with`ReactDOMServer.renderToString`, the `code` property of the `status` object will be updated to be `404`if the `NotFound`screen was rendered.
201
+
202
+
You can follow a similar approach for other status codes too.
203
+
147
204
## Summary
148
205
149
206
- Use the `location` prop on `ServerContainer` to render correct screens based on the incoming request.
0 commit comments