博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Redux] Extracting Presentational Components -- Footer, FilterLink
阅读量:6273 次
发布时间:2019-06-22

本文共 6366 字,大约阅读时间需要 21 分钟。

Code to be refactored: 

let nextTodoId = 0;class TodoApp extends Component {  render() {    const {      todos,      visibilityFilter    } = this.props;    const visibleTodos = getVisibleTodos(      todos,      visibilityFilter    );    return (      
{ this.input = node; }} />
    {visibleTodos.map(todo =>
  • { store.dispatch({ type: 'TOGGLE_TODO', id: todo.id }); }} style={
    { textDecoration: todo.completed ? 'line-through' : 'none' }}> {todo.text}
  • )}

Show: {' '}

All
{', '}
Active
{', '}
Completed

); }}

 

const FilterLink = ({  filter,  currentFilter,  children}) => {  if (filter === currentFilter) {    return {children};  }  return (     {         e.preventDefault();         store.dispatch({           type: 'SET_VISIBILITY_FILTER',           filter         });       }}    >      {children}      );};

 

Refactor footer part into a functional component, which contains all these three filter links. Pass in visibilityFilter as props:

const Footer = ({  visibilityFilter}) => (  

Show: {

' '}
All
{', '}
Active
{', '}
Completed

);

 

In the FilterLink,  we want it to be presentational components. However, the filter link includes a short dispatch call. I am replacing it with an on click call. I pass the filter as the single parameter for the calling component's convenience. I add on click to the props.

const FilterLink = ({  filter,  currentFilter,  children,  onFilterClick}) => {  if (filter === currentFilter) {    return {children};  }  return (     {         e.preventDefault();         onFilterClick(filter);       }}    >      {children}      );};

 

const Footer = ({  visibilityFilter,  onFilterClick}) => (  

Show: {

' '}
All
{', '}
Active
{', '}
Completed

);

  -----------------------------------

 

Code:

const todo = (state, action) => {  switch (action.type) {    case 'ADD_TODO':      return {        id: action.id,        text: action.text,        completed: false      };    case 'TOGGLE_TODO':      if (state.id !== action.id) {        return state;      }      return {        ...state,        completed: !state.completed      };    default:      return state;  }};const todos = (state = [], action) => {  switch (action.type) {    case 'ADD_TODO':      return [        ...state,        todo(undefined, action)      ];    case 'TOGGLE_TODO':      return state.map(t =>        todo(t, action)      );    default:      return state;  }};const visibilityFilter = (  state = 'SHOW_ALL',  action) => {  switch (action.type) {    case 'SET_VISIBILITY_FILTER':      return action.filter;    default:      return state;  }};const { combineReducers } = Redux;const todoApp = combineReducers({  todos,  visibilityFilter});const { createStore } = Redux;const store = createStore(todoApp);const { Component } = React;/** Functional compoment, persental compoment: doesn't need to know what to do, just show the interface, call the callback function.*/const AddTodo = ({  onAddTodo}) => {    let input;  return (     
{ input = node; }} />
);}/* Functional component */const Footer = ({ visibilityFilter, onFilterClick}) => (

Show: {

' '}
All
{', '}
Active
{', '}
Completed

);const FilterLink = ({ filter, currentFilter, children, onFilterClick}) => { if (filter === currentFilter) { return {children}; } return ( { e.preventDefault(); onFilterClick(filter); }} > {children} );};const getVisibleTodos = ( todos, filter) => { switch (filter) { case 'SHOW_ALL': return todos; case 'SHOW_COMPLETED': return todos.filter( t => t.completed ); case 'SHOW_ACTIVE': return todos.filter( t => !t.completed ); }}let nextTodoId = 0;class TodoApp extends Component { render() { const { todos, visibilityFilter } = this.props; const visibleTodos = getVisibleTodos( todos, visibilityFilter ); return (
store.dispatch({ type: 'ADD_TODO', id: nextTodoId++, text }) } />
    {visibleTodos.map(todo =>
  • { store.dispatch({ type: 'TOGGLE_TODO', id: todo.id }); }} style={
    { textDecoration: todo.completed ? 'line-through' : 'none' }}> {todo.text}
  • )}
{ store.dispatch({ type: 'SET_VISIBILITY_FILTER', filter }); }} />
); }}const render = () => { ReactDOM.render(
, document.getElementById('root') );};store.subscribe(render);render();

 

转载地址:http://hgopa.baihongyu.com/

你可能感兴趣的文章
智能照明造福千家万户 家居智能不再是梦
查看>>
物联网如何跳出“看起来很美”?
查看>>
浅谈MySQL 数据库性能优化
查看>>
拥抱白帽黑客,通用宣布安全漏洞报告项目
查看>>
《UNIX/Linux 系统管理技术手册(第四版)》——1.10 其他的权威文档
查看>>
灵动空间 创享生活
查看>>
《UNIX网络编程 卷1:套接字联网API(第3版)》——8.6 UDP回射客户程序:dg_cli函数...
查看>>
不要将时间浪费到编写完美代码上
查看>>
《第一桶金怎么赚——淘宝开店创业致富一册通》一一第1章 创业梦想,怎样起步...
查看>>
基于容器服务的持续集成与云端交付(三)- 从零搭建持续交付系统
查看>>
《算法基础:打开算法之门》一3.4 归并排序
查看>>
高德开放平台开放源代码 鼓励开发者创新
查看>>
《高并发Oracle数据库系统的架构与设计》一2.5 索引维护
查看>>
《Exchange Server 2010 SP1/SP2管理实践》——2.4 部署外部网络环境
查看>>
Firefox 是 Pwn2own 2014 上攻陷次数最多的浏览器
查看>>
《Java多线程编程核心技术》——1.8节暂停线程
查看>>
阿里感悟(十八)- 应届生Review
查看>>
《计算广告:互联网商业变现的市场与技术》一第一部分 在线广告市场与背景...
查看>>
话说模式匹配(5) for表达式中的模式匹配
查看>>
《锋利的SQL(第2版)》——1.7 常用函数
查看>>