The code to be refactored:

let nextTodoId = 0;
class TodoApp extends Component {
render() {
const {
todos,
visibilityFilter
} = this.props;
const visibleTodos = getVisibleTodos(
todos,
visibilityFilter
);
return (
<div>
<input ref={node => {
this.input = node;
}} />
<button onClick={() => {
store.dispatch({
type: 'ADD_TODO',
text: this.input.value,
id: nextTodoId++
});
this.input.value = '';
}}>
Add Todo
</button>
<ul>
{visibleTodos.map(todo =>
<li key={todo.id}
onClick={() => {
store.dispatch({
type: 'TOGGLE_TODO',
id: todo.id
});
}}
style={{
textDecoration:
todo.completed ?
'line-through' :
'none'
}}>
{todo.text}
</li>
)}
</ul>
<p>
Show:
{' '}
<FilterLink
filter='SHOW_ALL'
currentFilter={visibilityFilter}
>
All
</FilterLink>
{', '}
<FilterLink
filter='SHOW_ACTIVE'
currentFilter={visibilityFilter}
>
Active
</FilterLink>
{', '}
<FilterLink
filter='SHOW_COMPLETED'
currentFilter={visibilityFilter}
>
Completed
</FilterLink>
</p>
</div>
);
}
}

We extracting the Add todo input and button to a functional component, the functional components don't have instances. So we remove

this.input

Also  I want it to be a presentational component and not specify behavior, so I just called the function called, "AddTodo," passing the current input value. I make on at click a prop so that the component that uses OnAddTodo can specify what happens when that button is clicked.

const AddTodo = ({
onAddTodo
}) => { let input;
return (
<div>
<input ref={node => {
input = node;
}} />
<button onClick={() => {
onAddTodo(input.value);
input.value = '';
}}>
Add Todo
</button>
</div>
);
}
let nextTodoId = 0;
class TodoApp extends Component {
render() {
const {
todos,
visibilityFilter
} = this.props;
const visibleTodos = getVisibleTodos(
todos,
visibilityFilter
);
return (
<div>
<AddTodo
onAddTodo={ text =>
store.dispatch({
type: 'ADD_TODO',
id: nextTodoId++,
text
})
}
></AddTodo>
<ul>
{visibleTodos.map(todo =>
<li key={todo.id}
onClick={() => {
store.dispatch({
type: 'TOGGLE_TODO',
id: todo.id
});
}}
style={{
textDecoration:
todo.completed ?
'line-through' :
'none'
}}>
{todo.text}
</li>
)}
</ul>
<p>
Show:
{' '}
<FilterLink
filter='SHOW_ALL'
currentFilter={visibilityFilter}
>
All
</FilterLink>
{', '}
<FilterLink
filter='SHOW_ACTIVE'
currentFilter={visibilityFilter}
>
Active
</FilterLink>
{', '}
<FilterLink
filter='SHOW_COMPLETED'
currentFilter={visibilityFilter}
>
Completed
</FilterLink>
</p>
</div>
);
}
}

最新文章

  1. IOS定位
  2. 屏蔽zencart logs文件夹下不断生成的日志文件
  3. bootstrap插件学习-bootstrap.tooltip.js
  4. 在Eclipse中查看JDK类库的源代码
  5. Java TCP异步数据接收
  6. 引用的时候js不能使用虚拟路劲,调试时用排除法测试
  7. 有return的情况下try catch finally的执行顺序(最有说服力的总结)
  8. html5刮刮卡
  9. properties文件的继承(套用)关系
  10. [BZOJ 1047] [HAOI2007] 理想的正方形 【单调队列】
  11. What's New in C# 6.0(转)
  12. 【一天一道LeetCode】#328 Odd Even Linked List
  13. 在基于Windows系统的PHP虚拟主机上实现域名的301永久重定向
  14. zepto和jQuery on事件委托在苹果手机上的”坑“
  15. Tensorflow选择性初始化图中的变量
  16. git的使用方式总结
  17. 【Unity】通用的Debugger日志模块
  18. luogu P3198 [HNOI2008]遥远的行星
  19. Ubuntu学习总结-01 安装Ubuntu
  20. git——添加远程库

热门文章

  1. java单例模式(两种常用模式)
  2. sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
  3. 去除html页面中按钮在ios中的默认样式,去除select自带的小三角图标
  4. FeatureClass的&quot;import&quot;(转换)功能
  5. angular请求传递不了数据
  6. web标准(复习)--2 列布局
  7. php目录分隔符DIRECTORY_SEPARATOR
  8. sql基础复习
  9. Thinking in Java——笔记(21)
  10. Qt C++中的关键字explicit——防止隐式转换(也就是Java里的装箱),必须写清楚