@@ -11,10 +11,10 @@ import React from 'react'
1111import { render } from ' react-dom'
1212import { Provider } from ' react-redux'
1313import { createStore } from ' redux'
14- import todoApp from ' ./reducers'
14+ import reducer from ' ./reducers'
1515import App from ' ./components/App'
1616
17- let store = createStore (todoApp )
17+ const store = createStore (reducer )
1818
1919render (
2020 < Provider store= {store}>
@@ -30,27 +30,21 @@ render(
3030
3131``` js
3232let nextTodoId = 0
33- export const addTodo = text => {
34- return {
35- type: ' ADD_TODO' ,
36- id: nextTodoId++ ,
37- text
38- }
39- }
33+ export const addTodo = text => ({
34+ type: ' ADD_TODO' ,
35+ id: nextTodoId++ ,
36+ text
37+ })
4038
41- export const setVisibilityFilter = filter => {
42- return {
43- type: ' SET_VISIBILITY_FILTER' ,
44- filter
45- }
46- }
39+ export const setVisibilityFilter = filter => ({
40+ type: ' SET_VISIBILITY_FILTER' ,
41+ filter
42+ })
4743
48- export const toggleTodo = id => {
49- return {
50- type: ' TOGGLE_TODO' ,
51- id
52- }
53- }
44+ export const toggleTodo = id => ({
45+ type: ' TOGGLE_TODO' ,
46+ id
47+ })
5448
5549export const VisibilityFilters = {
5650 SHOW_ALL : ' SHOW_ALL' ,
@@ -154,11 +148,15 @@ import React from 'react'
154148import PropTypes from ' prop-types'
155149import Todo from ' ./Todo'
156150
157- const TodoList = ({ todos, onTodoClick }) => (
151+ const TodoList = ({ todos, toggleTodo }) => (
158152 < ul>
159- {todos .map (todo => (
160- < Todo key= {todo .id } {... todo} onClick= {() => onTodoClick (todo .id )} / >
161- ))}
153+ {todos .map (todo =>
154+ < Todo
155+ key= {todo .id }
156+ {... todo}
157+ onClick= {() => toggleTodo (todo .id )}
158+ / >
159+ )}
162160 < / ul>
163161)
164162
@@ -170,7 +168,7 @@ TodoList.propTypes = {
170168 text: PropTypes .string .isRequired
171169 }).isRequired
172170 ).isRequired ,
173- onTodoClick : PropTypes .func .isRequired
171+ toggleTodo : PropTypes .func .isRequired
174172}
175173
176174export default TodoList
@@ -181,23 +179,17 @@ export default TodoList
181179import React from ' react'
182180import PropTypes from ' prop-types'
183181
184- const Link = ({ active, children, onClick }) => {
185- if (active) {
186- return < span> {children}< / span>
187- }
188-
189- return (
190- < a
191- href= " "
192- onClick= {e => {
193- e .preventDefault ()
194- onClick ()
195- }}
196- >
197- {children}
198- < / a>
199- )
200- }
182+ const Link = ({ active, children, onClick }) => (
183+ < button
184+ onClick= {onClick}
185+ disabled= {active}
186+ style= {{
187+ marginLeft: ' 4px' ,
188+ }}
189+ >
190+ {children}
191+ < / button>
192+ )
201193
202194Link .propTypes = {
203195 active: PropTypes .bool .isRequired ,
@@ -216,21 +208,18 @@ import FilterLink from '../containers/FilterLink'
216208import { VisibilityFilters } from ' ../actions'
217209
218210const Footer = () => (
219- < p>
220- Show:
221- {' ' }
211+ < div>
212+ < span> Show: < / span>
222213 < FilterLink filter= {VisibilityFilters .SHOW_ALL }>
223214 All
224215 < / FilterLink>
225- {' , ' }
226216 < FilterLink filter= {VisibilityFilters .SHOW_ACTIVE }>
227217 Active
228218 < / FilterLink>
229- {' , ' }
230219 < FilterLink filter= {VisibilityFilters .SHOW_COMPLETED }>
231220 Completed
232221 < / FilterLink>
233- < / p >
222+ < / div >
234223)
235224
236225export default Footer
@@ -276,26 +265,18 @@ const getVisibleTodos = (todos, filter) => {
276265 }
277266}
278267
279- const mapStateToProps = state => {
280- return {
281- todos: getVisibleTodos (state .todos , state .visibilityFilter )
282- }
283- }
268+ const mapStateToProps = state => ({
269+ todos: getVisibleTodos (state .todos , state .visibilityFilter )
270+ })
284271
285- const mapDispatchToProps = dispatch => {
286- return {
287- onTodoClick : id => {
288- dispatch (toggleTodo (id))
289- }
290- }
291- }
272+ const mapDispatchToProps = dispatch => ({
273+ toggleTodo : id => dispatch (toggleTodo (id))
274+ })
292275
293- const VisibleTodoList = connect (
276+ export default connect (
294277 mapStateToProps,
295278 mapDispatchToProps
296279)(TodoList)
297-
298- export default VisibleTodoList
299280```
300281
301282#### ` containers/FilterLink.js `
@@ -305,26 +286,18 @@ import { connect } from 'react-redux'
305286import { setVisibilityFilter } from ' ../actions'
306287import Link from ' ../components/Link'
307288
308- const mapStateToProps = (state , ownProps ) => {
309- return {
310- active: ownProps .filter === state .visibilityFilter
311- }
312- }
289+ const mapStateToProps = (state , ownProps ) => ({
290+ active: ownProps .filter === state .visibilityFilter
291+ })
313292
314- const mapDispatchToProps = (dispatch , ownProps ) => {
315- return {
316- onClick : () => {
317- dispatch (setVisibilityFilter (ownProps .filter ))
318- }
319- }
320- }
293+ const mapDispatchToProps = (dispatch , ownProps ) => ({
294+ onClick : () => dispatch (setVisibilityFilter (ownProps .filter ))
295+ })
321296
322- const FilterLink = connect (
297+ export default connect (
323298 mapStateToProps,
324299 mapDispatchToProps
325300)(Link)
326-
327- export default FilterLink
328301```
329302
330303### Other Components
@@ -336,7 +309,7 @@ import React from 'react'
336309import { connect } from ' react-redux'
337310import { addTodo } from ' ../actions'
338311
339- let AddTodo = ({ dispatch }) => {
312+ const AddTodo = ({ dispatch }) => {
340313 let input
341314
342315 return (
@@ -351,19 +324,14 @@ let AddTodo = ({ dispatch }) => {
351324 input .value = ' '
352325 }}
353326 >
354- < input
355- ref= {node => {
356- input = node
357- }}
358- / >
327+ < input ref= {node => input = node} / >
359328 < button type= " submit" >
360329 Add Todo
361330 < / button>
362331 < / form>
363332 < / div>
364333 )
365334}
366- AddTodo = connect ()(AddTodo)
367335
368- export default AddTodo
336+ export default connect ()( AddTodo)
369337```
0 commit comments