[Redux] Extracting Presentational Components -- Todo, TodoList
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的更多相关文章
- [Redux] Extracting Presentational Components -- AddTodo
The code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { ...
- [Redux] Extracting Presentational Components -- Footer, FilterLink
Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...
- [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 ...
- [Redux] Extracting Container Components -- Complete
Clean TodoApp Component, it doesn't need to receive any props from the top level component: const To ...
- [Redux] Redux: Extracting Container Components -- AddTodo
Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...
- [Redux] Extracting Container Components (FilterLink)
Learn how to avoid the boilerplate of passing the props down the intermediate components by introduc ...
- [Redux] Extracting Container Components -- VisibleTodoList
Code to be refacted: const TodoList = ({ todos, onTodoClick }) => ( <ul> {todos.map(todo =& ...
- [Redux] Extracting Action Creators
We will create an anction creator to manage the dispatch actions, to keep code maintainable and self ...
- react+redux+generation-modation脚手架添加一个todolist
当我遇到问题: 要沉着冷静. 要管理好时间. 别被bug或error搞的不高兴,要高兴,又有煅炼思维的机会了. 要思考这是为什么? 要搞清楚问题的本质. 要探究问题,探究数据的流动. TodoList ...
随机推荐
- Linux中/etc/passwd文件与/etc/shadow文件解析.
此文章转载自"慧可",用来学习. 1. /etc/passwd文件 1.1 /etc/passwd文件内容格式 用户名: 密码 : uid : gid :用户描述:主目录:登陆s ...
- Javascript高级程序设计读书笔记(第10章 DOM)
第10章 DOM 10.1 节点层次 每个节点都有一个nodeType属性,用于表明节点的类型.任何节点类型必是下面中的一个: Node.Element_NODE(1); NODE.ATTRIBUT ...
- jQuery下的显示和隐藏
因为太久没更新了,所以来放一点没意思的内容. 做的是jQuery框架的隐藏和显示,HTML如下: <ul> <li>1</li> <li>2</l ...
- Geodatabase - 修改字段别名(Field Alias)
以下代码演示的是通过个人数据库打开要素类,并对指定的字段别名进行修改,其中,需要注意的是,不能通过Engine中的AxMapControl直接获得,如 //直接获得IFeatureClass. //E ...
- innodb部分参数优化
参数调优内容:1. 内存利用方面2. 日值控制方面3. 文件IO分配,空间占用方面4. 其它相关参数 1.内存利用方面:首先介绍一个Innodb最重要的参数:innodb_buffer_pool_si ...
- web离线应用
其实浏览器一直都是前端开发人员比较头痛的一个课题,各种兼容性.直到开始规范化和标准化,HTML5的出现. 在没有这套标准出现之前,对于客户端缓存的问题,我们基本上都是用cookie处理,当然目前除了后 ...
- [Unity优化] Unity CPU性能优化
前段时间本人转战unity手游,由于作者(Chwen)之前参与端游开发,有些端游的经验可以直接移植到手游,比如项目框架架构.代码设计.部分性能分析,而对于移动终端而言,CPU.内存.显卡甚至电池等硬件 ...
- 移动端-jquery Ajax执行滑动到底部进行加载 指定加载次数
过完年了,又要有新的项目了,趁暴风雨前的宁静,就把之前让我挺头疼的ajax加载给总结了一下.在此分享. 声明下:笔者对于ajax也是新手,如有更好的写法或可以优化的写法,还望指正! 项目需求: 移动端 ...
- 转载:mysql-Auto_increment值修改
转载网址:http://libo93122.blog.163.com/blog/static/1221893820125282158745/ | 2012-03-13 11:19:10 | 2012- ...
- 实例讲解MySQL联合查询
好了终于贴完了MySQL联合查询的内容了,加上上一篇一共2篇,都是我转载的,实例讲解MySQL联合查询.那下面就具体讲讲简单的JOIN的用法了.首先我们假设有2个表A和B,他们的表结构和字段分别为: ...