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>
);
}
}

First extact a single Todo as a persental component: which doesn't know what to do, just response for showing the interface:

So it accept a callback function to the onClick handler:

//remove
onClick={() => {
store.dispatch({
type: 'TOGGLE_TODO',
id: todo.id
});
}} -------------
// replace onClick={onTodoClick}
const Todo = ({
text,
completed,
onTodoClick
})=>{
return (
<li
onClick={onTodoClick}
style={{
textDecoration:
completed ?
'line-through' :
'none'
}}>
{text}
</li>
);
}

TodoList, should also be a persentional component:

const TodoList = ({
todos,
onTodoClick
}) => {
return (
<ul>
{todos.map(todo =>
<Todo
key={todo.id}
{...todo}
onClick={
()=>{
onTodoClick
}
}
/>
)}
</ul>
);
}

The TodoApp is the 'contianer component' which tell 'persentional componet' what to display and the action to dispatch.

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
})
}
/>
<TodoList
todos={visibleTodos}
onTodoClick={
(id)=>{
store.dispatch({
type: 'TOGGLE_TODO',
id
})
}
}
/>
<Footer
visibilityFilter = {visibilityFilter}
onFilterClick={ (filter) => {
store.dispatch({
type: 'SET_VISIBILITY_FILTER',
filter
});
}}
/>
</div>
);
}
}

in a word:

Presentaional compnent doesn't need to know what to do, only response for display.

Container component dispatch the action, and pass down to the persentional component.

[Redux] Extracting Presentational Components -- Todo, TodoList的更多相关文章

  1. [Redux] Extracting Presentational Components -- AddTodo

    The code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { ...

  2. [Redux] Extracting Presentational Components -- Footer, FilterLink

    Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...

  3. [Redux] Extracting Presentational Components -- TodoApp

    Finally, I just noticed that the to-do app component doesn't actually have to be a class. I can turn ...

  4. [Redux] Extracting Container Components -- Complete

    Clean TodoApp Component, it doesn't need to receive any props from the top level component: const To ...

  5. [Redux] Redux: Extracting Container Components -- AddTodo

    Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...

  6. [Redux] Extracting Container Components (FilterLink)

    Learn how to avoid the boilerplate of passing the props down the intermediate components by introduc ...

  7. [Redux] Extracting Container Components -- VisibleTodoList

    Code to be refacted: const TodoList = ({ todos, onTodoClick }) => ( <ul> {todos.map(todo =& ...

  8. [Redux] Extracting Action Creators

    We will create an anction creator to manage the dispatch actions, to keep code maintainable and self ...

  9. react+redux+generation-modation脚手架添加一个todolist

    当我遇到问题: 要沉着冷静. 要管理好时间. 别被bug或error搞的不高兴,要高兴,又有煅炼思维的机会了. 要思考这是为什么? 要搞清楚问题的本质. 要探究问题,探究数据的流动. TodoList ...

随机推荐

  1. opencv和javacv版本不一致

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniopencv_highgui in java.li ...

  2. 【iOS开发-从网络上获取图片尺寸】

    实际开发过程中,容易碰到从网络上获取图片尺寸的场景,比如一个UIImageView要装载从网络上获取的图片,但要先设置其frame,此时又不知道图片尺寸,就要从网络上获取尺寸了.为了最好的用户体验,一 ...

  3. Handler Looper 原理 详解

    演示代码 public class MainActivity extends ListActivity {     private TextView tv_info;     private CalT ...

  4. 使用SQL语句创建和删除约束

    原文:http://blog.csdn.net/hamber_bao/article/details/6504905 约束的目的就是确保表中的数据的完整性. 常用的约束类型如下: 主键约束:(Prim ...

  5. Action<T> 和 Func<T> 委托

    概述: 除了为每个参数和返回类型定义一个新委托类型之外,可以使用Action<T> 和 Func<T> 委托. Action<T> Action<T>委 ...

  6. iOS_SN_地图的使用(3)

    地图的定位,记得不用定位的时候要关掉定位不然会一直定位,使电量使用过快. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...

  7. jQuery Alert Dialogs (Alert, Confirm, & Prompt代替方案)

    基本范例--原文:http://keleyi.com/keleyi/phtml/jqplug/ Alert jAlert('自定义对话框', 'Alert对话框'); Confirm jConfirm ...

  8. Mysql学习(慕课学习笔记9)查询、分组

    查找记录 Select select username,id from users; Group by 进行分组 select sex from users group by sex; 分组条件 se ...

  9. SpringMVC项目中中文字符乱码问题及解决办法总结(非专业最优解决办法) -- ajax传值乱码; request.getParameter()乱码;

    情况一: ajax中传值时是乱码(后台可以获取到中文字符,但用@ResponseBody返回时前台为乱码) 情况二: Controller 中 request.getParameter()获取到的是乱 ...

  10. Mediawiki.org的PHP编码约定

    http://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP assignment作为expression来用看起来像个错误(looks su ...