Skip to content

Commit 496e96b

Browse files
ChrisJamesCseantcoyote
authored andcommitted
Use more ES6 syntaxes in the examples (reduxjs#1965)
* Style todos-with-undo examples * style todos example * style tree-view example * style universal example
1 parent 79baccc commit 496e96b

File tree

16 files changed

+137
-197
lines changed

16 files changed

+137
-197
lines changed
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
let nextTodoId = 0
2-
export const addTodo = (text) => {
3-
return {
4-
type: 'ADD_TODO',
5-
id: nextTodoId++,
6-
text
7-
}
8-
}
2+
export const addTodo = (text) => ({
3+
type: 'ADD_TODO',
4+
id: nextTodoId++,
5+
text
6+
})
97

10-
export const setVisibilityFilter = (filter) => {
11-
return {
12-
type: 'SET_VISIBILITY_FILTER',
13-
filter
14-
}
15-
}
8+
export const setVisibilityFilter = (filter) => ({
9+
type: 'SET_VISIBILITY_FILTER',
10+
filter
11+
})
1612

17-
export const toggleTodo = (id) => {
18-
return {
19-
type: 'TOGGLE_TODO',
20-
id
21-
}
22-
}
13+
export const toggleTodo = (id) => ({
14+
type: 'TOGGLE_TODO',
15+
id
16+
})

examples/todos-with-undo/src/containers/FilterLink.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ import { connect } from 'react-redux'
22
import { setVisibilityFilter } from '../actions'
33
import Link from '../components/Link'
44

5-
const mapStateToProps = (state, ownProps) => {
6-
return {
7-
active: ownProps.filter === state.visibilityFilter
8-
}
9-
}
5+
const mapStateToProps = (state, ownProps) => ({
6+
active: ownProps.filter === state.visibilityFilter
7+
})
108

11-
const mapDispatchToProps = (dispatch, ownProps) => {
12-
return {
13-
onClick: () => {
14-
dispatch(setVisibilityFilter(ownProps.filter))
15-
}
9+
const mapDispatchToProps = (dispatch, ownProps) => ({
10+
onClick: () => {
11+
dispatch(setVisibilityFilter(ownProps.filter))
1612
}
17-
}
13+
})
1814

1915
const FilterLink = connect(
2016
mapStateToProps,

examples/todos-with-undo/src/containers/UndoRedo.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,15 @@ let UndoRedo = ({ canUndo, canRedo, onUndo, onRedo }) => (
1313
</p>
1414
)
1515

16-
const mapStateToProps = (state) => {
17-
return {
18-
canUndo: state.todos.past.length > 0,
19-
canRedo: state.todos.future.length > 0
20-
}
21-
}
16+
const mapStateToProps = (state) => ({
17+
canUndo: state.todos.past.length > 0,
18+
canRedo: state.todos.future.length > 0
19+
})
2220

23-
const mapDispatchToProps = (dispatch) => {
24-
return {
25-
onUndo: () => dispatch(UndoActionCreators.undo()),
26-
onRedo: () => dispatch(UndoActionCreators.redo())
27-
}
28-
}
21+
const mapDispatchToProps = ({
22+
onUndo: UndoActionCreators.undo,
23+
onRedo: UndoActionCreators.redo
24+
})
2925

3026
UndoRedo = connect(
3127
mapStateToProps,

examples/todos-with-undo/src/containers/VisibleTodoList.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,13 @@ const getVisibleTodos = (todos, filter) => {
1515
}
1616
}
1717

18-
const mapStateToProps = (state) => {
19-
return {
20-
todos: getVisibleTodos(state.todos.present, state.visibilityFilter)
21-
}
22-
}
18+
const mapStateToProps = (state) => ({
19+
todos: getVisibleTodos(state.todos.present, state.visibilityFilter)
20+
})
2321

24-
const mapDispatchToProps = (dispatch) => {
25-
return {
26-
onTodoClick: (id) => {
27-
dispatch(toggleTodo(id))
28-
}
29-
}
30-
}
22+
const mapDispatchToProps = ({
23+
onTodoClick: toggleTodo
24+
})
3125

3226
const VisibleTodoList = connect(
3327
mapStateToProps,

examples/todos/src/actions/index.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
let nextTodoId = 0
2-
export const addTodo = (text) => {
3-
return {
4-
type: 'ADD_TODO',
5-
id: nextTodoId++,
6-
text
7-
}
8-
}
2+
export const addTodo = (text) => ({
3+
type: 'ADD_TODO',
4+
id: nextTodoId++,
5+
text
6+
})
97

10-
export const setVisibilityFilter = (filter) => {
11-
return {
12-
type: 'SET_VISIBILITY_FILTER',
13-
filter
14-
}
15-
}
8+
export const setVisibilityFilter = (filter) => ({
9+
type: 'SET_VISIBILITY_FILTER',
10+
filter
11+
})
1612

17-
export const toggleTodo = (id) => {
18-
return {
19-
type: 'TOGGLE_TODO',
20-
id
21-
}
22-
}
13+
export const toggleTodo = (id) => ({
14+
type: 'TOGGLE_TODO',
15+
id
16+
})

examples/todos/src/containers/FilterLink.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ import { connect } from 'react-redux'
22
import { setVisibilityFilter } from '../actions'
33
import Link from '../components/Link'
44

5-
const mapStateToProps = (state, ownProps) => {
6-
return {
7-
active: ownProps.filter === state.visibilityFilter
8-
}
9-
}
5+
const mapStateToProps = (state, ownProps) => ({
6+
active: ownProps.filter === state.visibilityFilter
7+
})
108

11-
const mapDispatchToProps = (dispatch, ownProps) => {
12-
return {
13-
onClick: () => {
14-
dispatch(setVisibilityFilter(ownProps.filter))
15-
}
9+
const mapDispatchToProps = (dispatch, ownProps) => ({
10+
onClick: () => {
11+
dispatch(setVisibilityFilter(ownProps.filter))
1612
}
17-
}
13+
})
1814

1915
const FilterLink = connect(
2016
mapStateToProps,

examples/todos/src/containers/VisibleTodoList.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,13 @@ const getVisibleTodos = (todos, filter) => {
1515
}
1616
}
1717

18-
const mapStateToProps = (state) => {
19-
return {
20-
todos: getVisibleTodos(state.todos, state.visibilityFilter)
21-
}
22-
}
18+
const mapStateToProps = (state) => ({
19+
todos: getVisibleTodos(state.todos, state.visibilityFilter)
20+
})
2321

24-
const mapDispatchToProps = (dispatch) => {
25-
return {
26-
onTodoClick: (id) => {
27-
dispatch(toggleTodo(id))
28-
}
29-
}
30-
}
22+
const mapDispatchToProps = ({
23+
onTodoClick: toggleTodo
24+
})
3125

3226
const VisibleTodoList = connect(
3327
mapStateToProps,

examples/tree-view/src/actions/index.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,30 @@ export const DELETE_NODE = 'DELETE_NODE'
44
export const ADD_CHILD = 'ADD_CHILD'
55
export const REMOVE_CHILD = 'REMOVE_CHILD'
66

7-
export function increment(nodeId) {
8-
return {
9-
type: INCREMENT,
10-
nodeId
11-
}
12-
}
7+
export const increment = (nodeId) => ({
8+
type: INCREMENT,
9+
nodeId
10+
})
1311

1412
let nextId = 0
15-
export function createNode() {
16-
return {
17-
type: CREATE_NODE,
18-
nodeId: `new_${nextId++}`
19-
}
20-
}
13+
export const createNode = () => ({
14+
type: CREATE_NODE,
15+
nodeId: `new_${nextId++}`
16+
})
2117

22-
export function deleteNode(nodeId) {
23-
return {
24-
type: DELETE_NODE,
25-
nodeId
26-
}
27-
}
18+
export const deleteNode = (nodeId) => ({
19+
type: DELETE_NODE,
20+
nodeId
21+
})
2822

29-
export function addChild(nodeId, childId) {
30-
return {
31-
type: ADD_CHILD,
32-
nodeId,
33-
childId
34-
}
35-
}
23+
export const addChild = (nodeId, childId) => ({
24+
type: ADD_CHILD,
25+
nodeId,
26+
childId
27+
})
3628

37-
export function removeChild(nodeId, childId) {
38-
return {
39-
type: REMOVE_CHILD,
40-
nodeId,
41-
childId
42-
}
43-
}
29+
export const removeChild = (nodeId, childId) => ({
30+
type: REMOVE_CHILD,
31+
nodeId,
32+
childId
33+
})

examples/tree-view/src/containers/Node.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ export class Node extends Component {
4444
+
4545
</button>
4646
{' '}
47-
{typeof parentId !== 'undefined' ?
47+
{typeof parentId !== 'undefined' &&
4848
<a href="#" onClick={this.handleRemoveClick}
4949
style={{ color: 'lightgray', textDecoration: 'none' }}>
5050
×
51-
</a> :
52-
null
51+
</a>
5352
}
5453
<ul>
5554
{childIds.map(this.renderChild)}

examples/tree-view/src/reducers/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { INCREMENT, ADD_CHILD, REMOVE_CHILD, CREATE_NODE, DELETE_NODE } from '../actions'
22

3-
function childIds(state, action) {
3+
const childIds = (state, action) => {
44
switch (action.type) {
55
case ADD_CHILD:
66
return [ ...state, action.childId ]
@@ -11,7 +11,7 @@ function childIds(state, action) {
1111
}
1212
}
1313

14-
function node(state, action) {
14+
const node = (state, action) => {
1515
switch (action.type) {
1616
case CREATE_NODE:
1717
return {
@@ -35,19 +35,19 @@ function node(state, action) {
3535
}
3636
}
3737

38-
function getAllDescendantIds(state, nodeId) {
39-
return state[nodeId].childIds.reduce((acc, childId) => (
38+
const getAllDescendantIds = (state, nodeId) => (
39+
state[nodeId].childIds.reduce((acc, childId) => (
4040
[ ...acc, childId, ...getAllDescendantIds(state, childId) ]
4141
), [])
42-
}
42+
)
4343

44-
function deleteMany(state, ids) {
44+
const deleteMany = (state, ids) => {
4545
state = { ...state }
4646
ids.forEach(id => delete state[id])
4747
return state
4848
}
4949

50-
export default function (state = {}, action) {
50+
export default (state = {}, action) => {
5151
const { nodeId } = action
5252
if (typeof nodeId === 'undefined') {
5353
return state

examples/universal/common/actions/index.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,31 @@ export const SET_COUNTER = 'SET_COUNTER'
22
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER'
33
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER'
44

5-
export function set(value) {
6-
return {
7-
type: SET_COUNTER,
8-
payload: value
9-
}
10-
}
5+
export const set = (value) => ({
6+
type: SET_COUNTER,
7+
payload: value
8+
})
119

12-
export function increment() {
13-
return {
14-
type: INCREMENT_COUNTER
15-
}
16-
}
10+
export const increment = () => ({
11+
type: INCREMENT_COUNTER
12+
})
1713

18-
export function decrement() {
19-
return {
20-
type: DECREMENT_COUNTER
21-
}
22-
}
14+
export const decrement = () => ({
15+
type: DECREMENT_COUNTER
16+
})
2317

24-
export function incrementIfOdd() {
25-
return (dispatch, getState) => {
26-
const { counter } = getState()
18+
export const incrementIfOdd = () => (dispatch, getState) => {
19+
const { counter } = getState()
2720

28-
if (counter % 2 === 0) {
29-
return
30-
}
31-
32-
dispatch(increment())
21+
if (counter % 2 === 0) {
22+
return
3323
}
24+
25+
dispatch(increment())
3426
}
3527

36-
export function incrementAsync(delay = 1000) {
37-
return dispatch => {
38-
setTimeout(() => {
39-
dispatch(increment())
40-
}, delay)
41-
}
28+
export const incrementAsync = (delay = 1000) => dispatch => {
29+
setTimeout(() => {
30+
dispatch(increment())
31+
}, delay)
4232
}

0 commit comments

Comments
 (0)