Browse Source

Replace vars with let and const (#8051)

main
Oskari Mantere 8 years ago
committed by Dan Abramov
parent
commit
97a1b89a94
  1. 10
      docs/addons-animation.md
  2. 6
      docs/addons-create-fragment.md
  3. 6
      docs/addons-test-utils.md
  4. 24
      docs/addons-update.md
  5. 14
      docs/jsx-in-depth.md
  6. 10
      docs/optimizing-performance.md
  7. 2
      docs/react-without-jsx.md
  8. 2
      docs/reference-events.md
  9. 2
      docs/typechecking-with-proptypes.md
  10. 8
      docs/web-components.md

10
docs/addons-animation.md

@ -31,20 +31,20 @@ class TodoList extends React.Component {
} }
handleAdd() { handleAdd() {
var newItems = this.state.items.concat([ const newItems = this.state.items.concat([
prompt('Enter some text') prompt('Enter some text')
]); ]);
this.setState({items: newItems}); this.setState({items: newItems});
} }
handleRemove(i) { handleRemove(i) {
var newItems = this.state.items.slice(); let newItems = this.state.items.slice();
newItems.splice(i, 1); newItems.splice(i, 1);
this.setState({items: newItems}); this.setState({items: newItems});
} }
render() { render() {
var items = this.state.items.map((item, i) => ( const items = this.state.items.map((item, i) => (
<div key={item} onClick={() => this.handleRemove(i)}> <div key={item} onClick={() => this.handleRemove(i)}>
{item} {item}
</div> </div>
@ -168,7 +168,7 @@ The example below would **not** work, because the `ReactCSSTransitionGroup` is b
```javascript{4,6,13} ```javascript{4,6,13}
render() { render() {
var items = this.state.items.map((item, i) => ( const items = this.state.items.map((item, i) => (
<div key={item} onClick={() => this.handleRemove(i)}> <div key={item} onClick={() => this.handleRemove(i)}>
<ReactCSSTransitionGroup transitionName="example"> <ReactCSSTransitionGroup transitionName="example">
{item} {item}
@ -263,7 +263,7 @@ However if you only need to render a single child inside `ReactTransitionGroup`,
```javascript ```javascript
function FirstChild(props) { function FirstChild(props) {
var childrenArray = React.Children.toArray(props.children); const childrenArray = React.Children.toArray(props.children);
return childrenArray[0] || null; return childrenArray[0] || null;
} }
``` ```

6
docs/addons-create-fragment.md

@ -24,7 +24,7 @@ That is, if you have a component such as:
```js ```js
function Swapper(props) { function Swapper(props) {
var children; let children;
if (props.swapped) { if (props.swapped) {
children = [props.rightChildren, props.leftChildren]; children = [props.rightChildren, props.leftChildren];
} else { } else {
@ -43,10 +43,10 @@ To solve this problem, you can use the `createFragment` add-on to give keys to t
Instead of creating arrays, we write: Instead of creating arrays, we write:
```js ```js
var createFragment = require('react-addons-create-fragment'); import createFragment from 'react-addons-create-fragment'
function Swapper(props) { function Swapper(props) {
var children; let children;
if (props.swapped) { if (props.swapped) {
children = createFragment({ children = createFragment({
right: props.rightChildren, right: props.rightChildren,

6
docs/addons-test-utils.md

@ -65,7 +65,7 @@ You can then begin to assert facts about the output. For example, if your compon
Then you can assert: Then you can assert:
```javascript ```javascript
var renderer = ReactTestUtils.createRenderer(); const renderer = ReactTestUtils.createRenderer();
result = renderer.getRenderOutput(); result = renderer.getRenderOutput();
expect(result.type).toBe('div'); expect(result.type).toBe('div');
expect(result.props.children).toEqual([ expect(result.props.children).toEqual([
@ -99,7 +99,7 @@ Simulate an event dispatch on a DOM node with optional `eventData` event data.
```javascript ```javascript
// <button ref="button">...</button> // <button ref="button">...</button>
var node = this.refs.button; const node = this.refs.button;
ReactTestUtils.Simulate.click(node); ReactTestUtils.Simulate.click(node);
``` ```
@ -107,7 +107,7 @@ ReactTestUtils.Simulate.click(node);
```javascript ```javascript
// <input ref="input" /> // <input ref="input" />
var node = this.refs.input; const node = this.refs.input;
node.value = 'giraffe'; node.value = 'giraffe';
ReactTestUtils.Simulate.change(node); ReactTestUtils.Simulate.change(node);
ReactTestUtils.Simulate.keyDown(node, {key: "Enter", keyCode: 13, which: 13}); ReactTestUtils.Simulate.keyDown(node, {key: "Enter", keyCode: 13, which: 13});

24
docs/addons-update.md

@ -36,7 +36,7 @@ myData.a.b.push(9);
You have no way of determining which data has changed since the previous copy has been overwritten. Instead, you need to create a new copy of `myData` and change only the parts of it that need to be changed. Then you can compare the old copy of `myData` with the new one in `shouldComponentUpdate()` using triple-equals: You have no way of determining which data has changed since the previous copy has been overwritten. Instead, you need to create a new copy of `myData` and change only the parts of it that need to be changed. Then you can compare the old copy of `myData` with the new one in `shouldComponentUpdate()` using triple-equals:
```js ```js
var newData = deepCopy(myData); const newData = deepCopy(myData);
newData.x.y.z = 7; newData.x.y.z = 7;
newData.a.b.push(9); newData.a.b.push(9);
``` ```
@ -44,7 +44,7 @@ newData.a.b.push(9);
Unfortunately, deep copies are expensive, and sometimes impossible. You can alleviate this by only copying objects that need to be changed and by reusing the objects that haven't changed. Unfortunately, in today's JavaScript this can be cumbersome: Unfortunately, deep copies are expensive, and sometimes impossible. You can alleviate this by only copying objects that need to be changed and by reusing the objects that haven't changed. Unfortunately, in today's JavaScript this can be cumbersome:
```js ```js
var newData = extend(myData, { const newData = extend(myData, {
x: extend(myData.x, { x: extend(myData.x, {
y: extend(myData.x.y, {z: 7}), y: extend(myData.x.y, {z: 7}),
}), }),
@ -61,7 +61,7 @@ While this is fairly performant (since it only makes a shallow copy of `log n` o
```js ```js
import update from 'react-addons-update'; import update from 'react-addons-update';
var newData = update(myData, { const newData = update(myData, {
x: {y: {z: {$set: 7}}}, x: {y: {z: {$set: 7}}},
a: {b: {$push: [9]}} a: {b: {$push: [9]}}
}); });
@ -85,16 +85,16 @@ The `$`-prefixed keys are called *commands*. The data structure they are "mutati
### Simple push ### Simple push
```js ```js
var initialArray = [1, 2, 3]; const initialArray = [1, 2, 3];
var newArray = update(initialArray, {$push: [4]}); // => [1, 2, 3, 4] const newArray = update(initialArray, {$push: [4]}); // => [1, 2, 3, 4]
``` ```
`initialArray` is still `[1, 2, 3]`. `initialArray` is still `[1, 2, 3]`.
### Nested collections ### Nested collections
```js ```js
var collection = [1, 2, {a: [12, 17, 15]}]; const collection = [1, 2, {a: [12, 17, 15]}];
var newCollection = update(collection, {2: {a: {$splice: [[1, 1, 13, 14]]}}}); const newCollection = update(collection, {2: {a: {$splice: [[1, 1, 13, 14]]}}});
// => [1, 2, {a: [12, 13, 14, 15]}] // => [1, 2, {a: [12, 13, 14, 15]}]
``` ```
This accesses `collection`'s index `2`, key `a`, and does a splice of one item starting from index `1` (to remove `17`) while inserting `13` and `14`. This accesses `collection`'s index `2`, key `a`, and does a splice of one item starting from index `1` (to remove `17`) while inserting `13` and `14`.
@ -102,16 +102,16 @@ This accesses `collection`'s index `2`, key `a`, and does a splice of one item s
### Updating a value based on its current one ### Updating a value based on its current one
```js ```js
var obj = {a: 5, b: 3}; const obj = {a: 5, b: 3};
var newObj = update(obj, {b: {$apply: function(x) {return x * 2;}}}); const newObj = update(obj, {b: {$apply: function(x) {return x * 2;}}});
// => {a: 5, b: 6} // => {a: 5, b: 6}
// This is equivalent, but gets verbose for deeply nested collections: // This is equivalent, but gets verbose for deeply nested collections:
var newObj2 = update(obj, {b: {$set: obj.b * 2}}); const newObj2 = update(obj, {b: {$set: obj.b * 2}});
``` ```
### (Shallow) Merge ### (Shallow) Merge
```js ```js
var obj = {a: 5, b: 3}; const obj = {a: 5, b: 3};
var newObj = update(obj, {$merge: {b: 6, c: 7}}); // => {a: 5, b: 6, c: 7} const newObj = update(obj, {$merge: {b: 6, c: 7}}); // => {a: 5, b: 6, c: 7}
``` ```

14
docs/jsx-in-depth.md

@ -63,7 +63,7 @@ You can also refer to a React component using dot-notation from within JSX. This
```js ```js
import React from 'react'; import React from 'react';
var MyComponents = { const MyComponents = {
DatePicker: function(props) { DatePicker: function(props) {
return <div>imagine a {props.color} datepicker here</div>; return <div>imagine a {props.color} datepicker here</div>;
} }
@ -111,7 +111,7 @@ function Story1(props) {
} }
function render2(props) { function render2(props) {
var MyComponent = components[props.story]; const MyComponent = components[props.story];
// Valid JSX // Valid JSX
return <MyComponent />; return <MyComponent />;
@ -136,7 +136,7 @@ For `MyComponent`, The value of `props.foo` will be `10` because the expression
```js ```js
function NumberDescriber(props) { function NumberDescriber(props) {
var description; let description;
if (props.number % 2 == 0) { if (props.number % 2 == 0) {
description = <strong>even</strong>; description = <strong>even</strong>;
} else { } else {
@ -184,7 +184,7 @@ If you already have `props` as an object, and you want to pass it in JSX, you ca
```js ```js
function render1() { function render1() {
var props = {left: 'ben', right: 'hector'}; const props = {left: 'ben', right: 'hector'};
return <MyComponent {...props} />; return <MyComponent {...props} />;
} }
@ -276,7 +276,7 @@ function Item(props) {
} }
function renderTodoList() { function renderTodoList() {
var todos = ['finish doc', 'submit pr', 'nag dan to review']; const todos = ['finish doc', 'submit pr', 'nag dan to review'];
return ( return (
<ul> <ul>
{todos.map((message) => <Item key={message} message={message} />)} {todos.map((message) => <Item key={message} message={message} />)}
@ -306,8 +306,8 @@ function ListOfTenThings() {
// Calls the children callback numTimes to produce a repeated component // Calls the children callback numTimes to produce a repeated component
function Repeat(props) { function Repeat(props) {
var items = []; let items = [];
for (var i = 0; i < numTimes; i++) { for (let i = 0; i < numTimes; i++) {
items.push(props.children(i)); items.push(props.children(i));
} }
return <div>{items}</div> return <div>{items}</div>

10
docs/optimizing-performance.md

@ -208,8 +208,8 @@ If you're using Create React App, both `Object.assign` and the object spread syn
Immutability makes tracking changes cheap. A change will always result in a new object so we only need to check if the reference to the object has changed. For example, in this regular JavaScript code: Immutability makes tracking changes cheap. A change will always result in a new object so we only need to check if the reference to the object has changed. For example, in this regular JavaScript code:
```javascript ```javascript
var x = { foo: "bar" }; const x = { foo: "bar" };
var y = x; const y = x;
y.foo = "baz"; y.foo = "baz";
x === y; // true x === y; // true
``` ```
@ -217,9 +217,9 @@ x === y; // true
Although `y` was edited, since it's a reference to the same object as `x`, this comparison returns `true`. You can write similar code with immutable.js: Although `y` was edited, since it's a reference to the same object as `x`, this comparison returns `true`. You can write similar code with immutable.js:
```javascript ```javascript
var SomeRecord = Immutable.Record({ foo: null }); const SomeRecord = Immutable.Record({ foo: null });
var x = new SomeRecord({ foo: 'bar' }); const x = new SomeRecord({ foo: 'bar' });
var y = x.set('foo', 'baz'); const y = x.set('foo', 'baz');
x === y; // false x === y; // false
``` ```

2
docs/react-without-jsx.md

@ -45,7 +45,7 @@ The component can either be provided as a string, or as a subclass of `React.Com
If you get tired of typing `React.createElement` so much, one common pattern is to assign a shorthand: If you get tired of typing `React.createElement` so much, one common pattern is to assign a shorthand:
```js ```js
var e = React.createElement; const e = React.createElement;
ReactDOM.render( ReactDOM.render(
e('div', null, 'Hello World'), e('div', null, 'Hello World'),

2
docs/reference-events.md

@ -45,7 +45,7 @@ As such, you cannot access the event in an asynchronous way.
function onClick(event) { function onClick(event) {
console.log(event); // => nullified object. console.log(event); // => nullified object.
console.log(event.type); // => "click" console.log(event.type); // => "click"
var eventType = event.type; // => "click" const eventType = event.type; // => "click"
setTimeout(function() { setTimeout(function() {
console.log(event.type); // => null console.log(event.type); // => null

2
docs/typechecking-with-proptypes.md

@ -115,7 +115,7 @@ With `React.PropTypes.element` you can specify that only a single child can be p
class MyComponent extends React.Component { class MyComponent extends React.Component {
render() { render() {
// This must be exactly one element or it will warn. // This must be exactly one element or it will warn.
var children = this.props.children; const children = this.props.children;
return ( return (
<div> <div>
{children} {children}

8
docs/web-components.md

@ -42,14 +42,14 @@ function BrickFlipbox() {
## Using React in your Web Components ## Using React in your Web Components
```javascript ```javascript
var proto = Object.create(HTMLElement.prototype, { const proto = Object.create(HTMLElement.prototype, {
attachedCallback: { attachedCallback: {
value: function() { value: function() {
var mountPoint = document.createElement('span'); const mountPoint = document.createElement('span');
this.createShadowRoot().appendChild(mountPoint); this.createShadowRoot().appendChild(mountPoint);
var name = this.getAttribute('name'); const name = this.getAttribute('name');
var url = 'https://www.google.com/search?q=' + encodeURIComponent(name); const url = 'https://www.google.com/search?q=' + encodeURIComponent(name);
ReactDOM.render(<a href={url}>{name}</a>, mountPoint); ReactDOM.render(<a href={url}>{name}</a>, mountPoint);
} }
} }

Loading…
Cancel
Save