@@ -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 }>
223- All
214+ All
224215 < / FilterLink>
225- {' , ' }
226216 < FilterLink filter= {VisibilityFilters .SHOW_ACTIVE }>
227- Active
217+ Active
228218 < / FilterLink>
229- {' , ' }
230219 < FilterLink filter= {VisibilityFilters .SHOW_COMPLETED }>
231- Completed
220+ Completed
232221 < / FilterLink>
233- < / p >
222+ < / div >
234223)
235224
236225export default Footer
@@ -282,20 +271,18 @@ const mapStateToProps = state => {
282271 }
283272}
284273
285- const mapDispatchToProps = dispatch => {
286- return {
287- onTodoClick : id => {
288- dispatch ( toggleTodo (id))
289- }
290- }
291- }
274+ const mapStateToProps = state => ( {
275+ todos : getVisibleTodos ( state . todos , state . visibilityFilter )
276+ })
277+
278+ const mapDispatchToProps = dispatch => ({
279+ toggleTodo : id => dispatch ( toggleTodo (id))
280+ })
292281
293- const VisibleTodoList = connect (
282+ export default connect (
294283 mapStateToProps,
295284 mapDispatchToProps
296285)(TodoList)
297-
298- export default VisibleTodoList
299286```
300287
301288#### ` containers/FilterLink.js `
@@ -305,26 +292,18 @@ import { connect } from 'react-redux'
305292import { setVisibilityFilter } from ' ../actions'
306293import Link from ' ../components/Link'
307294
308- const mapStateToProps = (state , ownProps ) => {
309- return {
310- active: ownProps .filter === state .visibilityFilter
311- }
312- }
295+ const mapStateToProps = (state , ownProps ) => ({
296+ active: ownProps .filter === state .visibilityFilter
297+ })
313298
314- const mapDispatchToProps = (dispatch , ownProps ) => {
315- return {
316- onClick : () => {
317- dispatch (setVisibilityFilter (ownProps .filter ))
318- }
319- }
320- }
299+ const mapDispatchToProps = (dispatch , ownProps ) => ({
300+ onClick : () => dispatch (setVisibilityFilter (ownProps .filter ))
301+ })
321302
322- const FilterLink = connect (
303+ export default connect (
323304 mapStateToProps,
324305 mapDispatchToProps
325306)(Link)
326-
327- export default FilterLink
328307```
329308
330309### Other Components
@@ -336,7 +315,7 @@ import React from 'react'
336315import { connect } from ' react-redux'
337316import { addTodo } from ' ../actions'
338317
339- let AddTodo = ({ dispatch }) => {
318+ const AddTodo = ({ dispatch }) => {
340319 let input
341320
342321 return (
@@ -351,19 +330,14 @@ let AddTodo = ({ dispatch }) => {
351330 input .value = ' '
352331 }}
353332 >
354- < input
355- ref= {node => {
356- input = node
357- }}
358- / >
333+ < input ref= {node => input = node} / >
359334 < button type= " submit" >
360335 Add Todo
361336 < / button>
362337 < / form>
363338 < / div>
364339 )
365340}
366- AddTodo = connect ()(AddTodo)
367341
368- export default AddTodo
342+ export default connect ()( AddTodo)
369343```
0 commit comments