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: docs/basics/Reducers.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -151,7 +151,7 @@ case COMPLETE_TODO:
151
151
});
152
152
```
153
153
154
-
Because we want to update a specific item in the array without resorting to mutations, we have to slice it before and after the item. If you find yourself often writing such operations, it’s a good idea to use a helper like [React.addons.update](https://facebook.github.io/react/docs/update.html), [updeep](https://github.com/substantial/updeep), or even a library like [Immutable](http://facebook.github.io/immutable-js/) that has native support for deep updates. Just remember to never assign to anything inside the `state` unless you clone it first.
154
+
Because we want to update a specific item in the array without resorting to mutations, we have to slice it before and after the item. If you find yourself often writing such operations, it’s a good idea to use a helper like [react-addons-update](https://facebook.github.io/react/docs/update.html), [updeep](https://github.com/substantial/updeep), or even a library like [Immutable](http://facebook.github.io/immutable-js/) that has native support for deep updates. Just remember to never assign to anything inside the `state` unless you clone it first.
Copy file name to clipboardExpand all lines: docs/recipes/ServerRendering.md
+15-10Lines changed: 15 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,19 +64,21 @@ The first thing that we need to do on every request is create a new Redux store
64
64
65
65
When rendering, we will wrap `<App />`, our root component, inside a `<Provider>` to make the store available to all components in the component tree, as we saw in [Usage with React](../basics/UsageWithReact.md).
66
66
67
-
The key step in server side rendering is to render the initial HTML of our component _**before**_ we send it to the client side. To do this, we use [React.renderToString()](https://facebook.github.io/react/docs/top-level-api.html#react.rendertostring).
67
+
The key step in server side rendering is to render the initial HTML of our component _**before**_ we send it to the client side. To do this, we use [ReactDOMServer.renderToString()](https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostring).
68
68
69
69
We then get the initial state from our Redux store using [`store.getState()`](../api/Store.md#getState). We will see how this is passed along in our `renderFullPage` function.
70
70
71
71
```js
72
+
import { renderToString } from'react-dom/server';
73
+
72
74
functionhandleRender(req, res) {
73
75
// Create a new Redux store instance
74
76
conststore=createStore(counterApp);
75
77
76
78
// Render the component to a string
77
-
consthtml=React.renderToString(
79
+
consthtml=renderToString(
78
80
<Provider store={store}>
79
-
{() =><App />}
81
+
<App />
80
82
</Provider>
81
83
);
82
84
@@ -130,6 +132,7 @@ Let’s take a look at our new client file:
You can set up your build tool of choice (Webpack, Browserify, etc.) to compile a bundle file into `dist/bundle.js`.
153
156
154
-
When the page loads, the bundle file will be started up and [`React.render()`](https://facebook.github.io/react/docs/top-level-api.html#react.render) will hook into the `data-react-id` attributes from the server-rendered HTML. This will connect our newly-started React instance to the virtual DOM used on the server. Since we have the same initial state for our Redux store and used the same code for all our view components, the result will be the same real DOM.
157
+
When the page loads, the bundle file will be started up and [`ReactDOM.render()`](https://facebook.github.io/react/docs/top-level-api.html#reactdom.render) will hook into the `data-react-id` attributes from the server-rendered HTML. This will connect our newly-started React instance to the virtual DOM used on the server. Since we have the same initial state for our Redux store and used the same code for all our view components, the result will be the same real DOM.
155
158
156
159
And that’s it! That is all we need to do to implement server side rendering.
157
160
@@ -171,6 +174,7 @@ The request contains information about the URL requested, including any query pa
171
174
172
175
```js
173
176
importqsfrom'qs'; // Add this at the top of the file
177
+
import { renderToString } from'react-dom/server';
174
178
175
179
functionhandleRender(req, res) {
176
180
// Read the counter from the request, if provided
@@ -184,9 +188,9 @@ function handleRender(req, res) {
184
188
conststore=createStore(counterApp, initialState);
185
189
186
190
// Render the component to a string
187
-
consthtml=React.renderToString(
191
+
consthtml=renderToString(
188
192
<Provider store={store}>
189
-
{() =><App />}
193
+
<App />
190
194
</Provider>
191
195
);
192
196
@@ -231,6 +235,7 @@ On the server side, we simply wrap our existing code in the `fetchCounter` and r
231
235
```js
232
236
// Add this to our imports
233
237
import { fetchCounter } from'./api/counter';
238
+
import { renderToString } from'react-dom/server';
234
239
235
240
functionhandleRender(req, res) {
236
241
// Query our mock API asynchronously
@@ -246,9 +251,9 @@ function handleRender(req, res) {
A nice thing about React components is that they are usually small and only rely on their props. That makes them easy to test.
254
254
255
+
First, we will install [React Test Utilities](https://facebook.github.io/react/docs/test-utils.html):
256
+
257
+
```
258
+
npm install --save-dev react-addons-test-utils
259
+
```
260
+
255
261
To test the components we make a `setup()` helper that passes the stubbed callbacks as props and renders the component with [React shallow renderer](https://facebook.github.io/react/docs/test-utils.html#shallow-rendering). This lets individual tests assert on whether the callbacks were called when expected.
It’s important that this code is evaluated *before* React is imported. To ensure this, modify your `mocha` command to include `--require ./test/setup.js` in the options.
368
+
It’s important that this code is evaluated *before* React is imported. To ensure this, modify your `mocha` command to include `--require ./test/setup.js` in the options in your `package.json`:
0 commit comments