Skip to content

Commit 5e32583

Browse files
committed
Merge pull request #901 from rackt/14
Update examples and docs to React 0.14
2 parents def86ae + 039bc00 commit 5e32583

File tree

26 files changed

+111
-94
lines changed

26 files changed

+111
-94
lines changed

docs/advanced/ExampleRedditAPI.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ This is the complete source code of the Reddit headline fetching example we buil
1010
import 'babel-core/polyfill';
1111

1212
import React from 'react';
13+
import { render } from 'react-dom';
1314
import Root from './containers/Root';
1415

15-
React.render(
16+
render(
1617
<Root />,
1718
document.getElementById('root')
1819
);
@@ -195,7 +196,7 @@ export default class Root extends Component {
195196
render() {
196197
return (
197198
<Provider store={store}>
198-
{() => <AsyncApp />}
199+
<AsyncApp />
199200
</Provider>
200201
);
201202
}

docs/api/applyMiddleware.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,12 @@ function makeSandwichesForEverybody() {
173173
// This is very useful for server side rendering, because I can wait
174174
// until data is available, then synchronously render the app.
175175

176+
import { renderToString } from 'react-dom/server';
177+
176178
store.dispatch(
177179
makeSandwichesForEverybody()
178180
).then(() =>
179-
response.send(React.renderToString(<MyApp store={store} />))
181+
response.send(renderToString(<MyApp store={store} />))
180182
);
181183

182184
// I can also dispatch a thunk async action from a component

docs/basics/ExampleTodoList.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This is the complete source code of the tiny todo app we built during the [basic
88

99
```js
1010
import React from 'react';
11+
import { render } from 'react-dom';
1112
import { createStore } from 'redux';
1213
import { Provider } from 'react-redux';
1314
import App from './containers/App';
@@ -16,11 +17,9 @@ import todoApp from './reducers';
1617
let store = createStore(todoApp);
1718

1819
let rootElement = document.getElementById('root');
19-
React.render(
20-
// The child must be wrapped in a function
21-
// to work around an issue in React 0.13.
20+
render(
2221
<Provider store={store}>
23-
{() => <App />}
22+
<App />
2423
</Provider>,
2524
rootElement
2625
);
@@ -190,7 +189,7 @@ export default connect(select)(App);
190189
#### `components/AddTodo.js`
191190

192191
```js
193-
import React, { findDOMNode, Component, PropTypes } from 'react';
192+
import React, { Component, PropTypes } from 'react';
194193

195194
export default class AddTodo extends Component {
196195
render() {
@@ -205,7 +204,7 @@ export default class AddTodo extends Component {
205204
}
206205

207206
handleClick(e) {
208-
const node = findDOMNode(this.refs.input);
207+
const node = this.refs.input;
209208
const text = node.value.trim();
210209
this.props.onAddClick(text);
211210
node.value = '';

docs/basics/Reducers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ case COMPLETE_TODO:
151151
});
152152
```
153153

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.
155155

156156
## Splitting Reducers
157157

docs/basics/UsageWithReact.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ These are all normal React components, so we won’t stop to examine them in det
8888
#### `components/AddTodo.js`
8989

9090
```js
91-
import React, { findDOMNode, Component, PropTypes } from 'react';
91+
import React, { Component, PropTypes } from 'react';
9292

9393
export default class AddTodo extends Component {
9494
render() {
@@ -103,7 +103,7 @@ export default class AddTodo extends Component {
103103
}
104104

105105
handleClick(e) {
106-
const node = findDOMNode(this.refs.input);
106+
const node = this.refs.input;
107107
const text = node.value.trim();
108108
this.props.onAddClick(text);
109109
node.value = '';
@@ -274,6 +274,7 @@ First, we need to import `Provider` from [`react-redux`](http://github.com/gaear
274274

275275
```js
276276
import React from 'react';
277+
import { render } from 'react-dom';
277278
import { createStore } from 'redux';
278279
import { Provider } from 'react-redux';
279280
import App from './containers/App';
@@ -282,11 +283,9 @@ import todoApp from './reducers';
282283
let store = createStore(todoApp);
283284

284285
let rootElement = document.getElementById('root');
285-
React.render(
286-
// The child must be wrapped in a function
287-
// to work around an issue in React 0.13.
286+
render(
288287
<Provider store={store}>
289-
{() => <App />}
288+
<App />
290289
</Provider>,
291290
rootElement
292291
);

docs/recipes/ServerRendering.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,21 @@ The first thing that we need to do on every request is create a new Redux store
6464

6565
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).
6666

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).
6868

6969
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.
7070

7171
```js
72+
import { renderToString } from 'react-dom/server';
73+
7274
function handleRender(req, res) {
7375
// Create a new Redux store instance
7476
const store = createStore(counterApp);
7577

7678
// Render the component to a string
77-
const html = React.renderToString(
79+
const html = renderToString(
7880
<Provider store={store}>
79-
{() => <App />}
81+
<App />
8082
</Provider>
8183
);
8284

@@ -130,6 +132,7 @@ Let’s take a look at our new client file:
130132

131133
```js
132134
import React from 'react';
135+
import { render } from 'react-dom';
133136
import { createStore } from 'redux';
134137
import { Provider } from 'react-redux';
135138
import App from './containers/App';
@@ -141,17 +144,17 @@ const initialState = window.__INITIAL_STATE__;
141144
// Create Redux store with initial state
142145
const store = createStore(counterApp, initialState);
143146

144-
React.render(
147+
render(
145148
<Provider store={store}>
146-
{() => <App />}
149+
<App />
147150
</Provider>,
148151
document.getElementById('root')
149152
);
150153
```
151154

152155
You can set up your build tool of choice (Webpack, Browserify, etc.) to compile a bundle file into `dist/bundle.js`.
153156

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.
155158

156159
And that’s it! That is all we need to do to implement server side rendering.
157160

@@ -171,6 +174,7 @@ The request contains information about the URL requested, including any query pa
171174

172175
```js
173176
import qs from 'qs'; // Add this at the top of the file
177+
import { renderToString } from 'react-dom/server';
174178

175179
function handleRender(req, res) {
176180
// Read the counter from the request, if provided
@@ -184,9 +188,9 @@ function handleRender(req, res) {
184188
const store = createStore(counterApp, initialState);
185189

186190
// Render the component to a string
187-
const html = React.renderToString(
191+
const html = renderToString(
188192
<Provider store={store}>
189-
{() => <App />}
193+
<App />
190194
</Provider>
191195
);
192196

@@ -231,6 +235,7 @@ On the server side, we simply wrap our existing code in the `fetchCounter` and r
231235
```js
232236
// Add this to our imports
233237
import { fetchCounter } from './api/counter';
238+
import { renderToString } from 'react-dom/server';
234239

235240
function handleRender(req, res) {
236241
// Query our mock API asynchronously
@@ -246,9 +251,9 @@ function handleRender(req, res) {
246251
const store = createStore(counterApp, initialState);
247252

248253
// Render the component to a string
249-
const html = React.renderToString(
254+
const html = renderToString(
250255
<Provider store={store}>
251-
{() => <App />}
256+
<App />
252257
</Provider>
253258
);
254259

docs/recipes/WritingTests.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ describe('todos reducer', () => {
252252

253253
A nice thing about React components is that they are usually small and only rely on their props. That makes them easy to test.
254254

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+
255261
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.
256262

257263
#### Example
@@ -290,13 +296,11 @@ can be tested like:
290296

291297
```js
292298
import expect from 'expect';
293-
import jsdomReact from '../jsdomReact';
294-
import React from 'react/addons';
299+
import React from 'react';
300+
import TestUtils from 'react-addons-test-utils';
295301
import Header from '../../components/Header';
296302
import TodoTextInput from '../../components/TodoTextInput';
297303

298-
const { TestUtils } = React.addons;
299-
300304
function setup() {
301305
let props = {
302306
addTodo: expect.createSpy()
@@ -314,8 +318,6 @@ function setup() {
314318
}
315319

316320
describe('components', () => {
317-
jsdomReact();
318-
319321
describe('Header', () => {
320322
it('should render correctly', () => {
321323
const { output } = setup();
@@ -363,7 +365,18 @@ global.window = document.defaultView;
363365
global.navigator = global.window.navigator;
364366
```
365367

366-
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`:
369+
370+
```js
371+
{
372+
...
373+
"scripts": {
374+
...
375+
"test": "mocha --compilers js:babel/register --recursive --require ./test/setup.js",
376+
},
377+
...
378+
}
379+
```
367380

368381
### Connected Components
369382

@@ -475,7 +488,7 @@ describe('middleware', () => {
475488

476489
### Glossary
477490

478-
- [React Test Utils](http://facebook.github.io/react/docs/test-utils.html): Test utilities that ship with React.
491+
- [React Test Utils](http://facebook.github.io/react/docs/test-utils.html): Test Utilities for React.
479492

480493
- [jsdom](https://github.com/tmpvar/jsdom): A plain JavaScript implementation of the DOM API. jsdom allows us to run the tests without browser.
481494

examples/async/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import 'babel-core/polyfill';
22
import React from 'react';
3+
import { render } from 'react-dom';
34
import { Provider } from 'react-redux';
45
import App from './containers/App';
56
import configureStore from './store/configureStore';
67

78
const store = configureStore();
89

9-
React.render(
10+
render(
1011
<Provider store={store}>
11-
{() => <App />}
12+
<App />
1213
</Provider>,
1314
document.getElementById('root')
1415
);

examples/async/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
"homepage": "http://rackt.github.io/redux",
2828
"dependencies": {
2929
"isomorphic-fetch": "^2.1.1",
30-
"react": "^0.13.3",
31-
"react-redux": "^2.1.2",
30+
"react": "^0.14.0",
31+
"react-dom": "^0.14.0",
32+
"react-redux": "^4.0.0",
3233
"redux": "^3.0.0",
3334
"redux-logger": "^2.0.2",
3435
"redux-thunk": "^0.1.0"

examples/counter/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React from 'react';
2+
import { render } from 'react-dom';
23
import { Provider } from 'react-redux';
34
import App from './containers/App';
45
import configureStore from './store/configureStore';
56

67
const store = configureStore();
78

8-
React.render(
9+
render(
910
<Provider store={store}>
10-
{() => <App />}
11+
<App />
1112
</Provider>,
1213
document.getElementById('root')
1314
);

0 commit comments

Comments
 (0)