forked from DageTeam/dage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_ES6-GUIDE.js
214 lines (161 loc) · 4.96 KB
/
_ES6-GUIDE.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************
* This is a VERY general and high level differences between ES5 and ES6 particularly in context of
* React and Redux. This assumes some basic knowledge of ES6.
******************/
/*******************
* require statements vs import statements. note that ES6 only pulls PropTypes and Component from react
******************/
//ES5
var React = require('react');
var PropTypes = React.PropTypes;
var TodoTextInput = require('./TodoTextInput');
//ES6
import React, { PropTypes, Component } from 'react';
import TodoTextInput from './TodoTextInput';
/*******************
* function definition within class declaraion. note the lack of "function" and commas between functions
******************/
//ES5
var Header = React.createClass({
handleSave: function(text) {
},
render: function() {
return (
<header>
</header>
);
},
});
//ES6
class Header extends Component {
handleSave(text) {
}
render() {
return (
<header>
</header>
);
}
}
/*******************
* React component initial state: note the differences and lack of 'getInitialState' within ES6. Because
* ES6 uses extends, you need to have a constructor AND the super(props, context) in order to establish
* the constructor/'child' relationship and gain access to props and context functionality
******************/
//ES5
var TodoItem = React.createClass({
getInitialState: function() {
return {
editing: false,
};
},
});
//ES6
class TodoItem extends Component {
constructor(props, context) {
super(props, context);
this.state = {
editing: false,
};
}
}
/*******************
* Anonymous functions replaced with => (please note that as per our style guide, we do not plan on using
* => for our anonymous function declarations. This is more for understanding Redux examples. The two
* function blocks below are equivilant.
******************/
//ES5
var markedCount = todos.reduce(function(count, todo) {
return todo.marked ? count + 1 : count;
}, 0);
//ES6
const markedCount = todos.reduce((count, todo) =>
todo.completed ? count + 1 : count, 0);
/*******************
* Ensuring components update within the reducer file: Please note 2 things. First is that the line for
* handling undefined arguments ('state = state || initialState') is now handled within the argments
* list.
* Second thing to note is that coding in ES5 would require .concat(state) to 'create' a new reference
* for the state change listeners (something part of Redux--ask team for help, or consult this guide:
* https://blog.andyet.com/2015/08/06/what-the-flux-lets-redux ). Notice that in ES6, ...state is used
* to create a new state variable within memory to ensure Redux's shouldComponentUpdate function is
* triggered
******************/
//ES5
module.exports = function todos(state, action) {
state = state || initialState
switch (action.type) {
case types.ADD_TODO:
return [{
id: (state.length === 0) ? 0 : state[0].id + 1,
marked: false,
text: action.text
}].concat(state);
}
}
//ES6
export default function todos(state = initialState, action) {
switch (action.type) {
case ADD_TODO:
return [{
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text: action.text
}, ...state];
}
}
/*******************
* Last line = return in ES6. I am personally not a fan of this
******************/
//ES5
return state.map(function(todo) {
return assign({}, todo, { marked: !areAllMarked })
});
//ES6
return state.map(todo => Object.assign({}, todo, {
completed: !areAllMarked
}));
/*******************
* Dynamic property names & template strings-we can now assign object literals to a derived
* property name. we have the ability to construct objects whose property names are determined
* by a JavaScript expression at runtime.
******************/
//ES5
var Form = React.createClass({
onChange: function(inputName, e) {
var stateToSet = {};
stateToSet[inputName + 'Value'] = e.target.value;
this.setState(stateToSet);
},
});
//ES6
class Form extends React.Component {
onChange(inputName, e) {
this.setState({
[`${inputName}Value`]: e.target.value,
});
}
}
/*******************
* Destructuring & spread attributes - when we want to pass down most of a parent component
* props to a child component, but not all of them.
******************/
//ES6
class MaximumPower extends React.Component {
render() {
var {
className,
...others, // contains all properties of this.props except for className
} = this.props;
return (
<div className={className}> //here, we can pass down just className
<Power {...others} /> //here, we pass down all properties of this.props except for className
</div>
);
}
}
/*******************
* what's the same: setting state
* TODO: add more examples as they come up
******************/
this.setState({ editing: false });