[Redux] Extracting Presentational Components -- Footer, FilterLink

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>
    );
  }
}
const FilterLink = ({
  filter,
  currentFilter,
  children
}) => {
  if (filter === currentFilter) {
    return <span>{children}</span>;
  }

  return (
    <a href=‘#‘
       onClick={e => {
         e.preventDefault();
         store.dispatch({
           type: ‘SET_VISIBILITY_FILTER‘,
           filter
         });
       }}
    >
      {children}
    </a>
  );
};

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

const Footer = ({
  visibilityFilter
}) => (
  <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>
);

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 <span>{children}</span>;
  }

  return (
    <a href=‘#‘
       onClick={e => {
         e.preventDefault();
         onFilterClick(filter);
       }}
    >
      {children}
    </a>
  );
};
const Footer = ({
  visibilityFilter,
  onFilterClick
}) => (
  <p>
          Show:
          {‘ ‘}
          <FilterLink
            filter=‘SHOW_ALL‘
            currentFilter={visibilityFilter}
            onFilterClick={onFilterClick}
          >
            All
          </FilterLink>
          {‘, ‘}
          <FilterLink
            filter=‘SHOW_ACTIVE‘
            currentFilter={visibilityFilter}
            onFilterClick={onFilterClick}
          >
            Active
          </FilterLink>
          {‘, ‘}
          <FilterLink
            filter=‘SHOW_COMPLETED‘
            currentFilter={visibilityFilter}
            onFilterClick={onFilterClick}
          >
            Completed
          </FilterLink>
        </p>
);

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

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 (
     <div>
          <input ref={node => {
          input = node;
        }} />
        <button onClick={() => {
          onAddTodo(input.value);
          input.value = ‘‘;
        }}>
          Add Todo
        </button>
     </div>
  );
}

/* Functional component */
const Footer = ({
  visibilityFilter,
  onFilterClick
}) => (
        <p>
          Show:
          {‘ ‘}
          <FilterLink
            filter=‘SHOW_ALL‘
            currentFilter={visibilityFilter}
            onFilterClick={onFilterClick}
          >
            All
          </FilterLink>
          {‘, ‘}
          <FilterLink
            filter=‘SHOW_ACTIVE‘
            currentFilter={visibilityFilter}
            onFilterClick={onFilterClick}
          >
            Active
          </FilterLink>
          {‘, ‘}
          <FilterLink
            filter=‘SHOW_COMPLETED‘
            currentFilter={visibilityFilter}
            onFilterClick={onFilterClick}
          >
            Completed
          </FilterLink>
        </p>
);

const FilterLink = ({
  filter,
  currentFilter,
  children,
  onFilterClick
}) => {
  if (filter === currentFilter) {
    return <span>{children}</span>;
  }

  return (
    <a href=‘#‘
       onClick={e => {
         e.preventDefault();
         onFilterClick(filter);
       }}
    >
      {children}
    </a>
  );
};

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 (
      <div>
        <AddTodo
           onAddTodo={ text =>
              store.dispatch({
                  type: ‘ADD_TODO‘,
                  id: nextTodoId++,
                  text
              })
           }
        />
        <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>
        <Footer
           visibilityFilter = {visibilityFilter}
           onFilterClick={ (filter) => {
              store.dispatch({
                 type: ‘SET_VISIBILITY_FILTER‘,
                 filter
             });
           }}
        />
      </div>
    );
  }
}

const render = () => {
  ReactDOM.render(
    <TodoApp
      {...store.getState()}
    />,
    document.getElementById(‘root‘)
  );
};

store.subscribe(render);
render();
时间: 2024-11-10 01:03:48

[Redux] Extracting Presentational Components -- Footer, FilterLink的相关文章

[Redux] Extracting Presentational Components -- AddTodo

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 => { t

[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 it into a function. I prefer to-do that when possible. Code to be refactored: class TodoApp extends Component { render() { const { todos, visibilityF

[Redux] Extracting Container Components (FilterLink)

Learn how to avoid the boilerplate of passing the props down the intermediate components by introducing more container components. Code to be refactored: const FilterLink = ({ filter, currentFilter, children, onClick }) => { if (filter === currentFil

Learning OpenCV Lecture 6 (Extracting Lines,Contours, and Components)

In this chapter, we will cover: Detecting image contours with the Canny operator Detecting lines in images with the Hough transform Fitting a line to a set of points Extracting the components' contours Computing components' shape descriptors Detectin

Redux管理你的React应用

使用Redux管理你的React应用 因为redux和react的版本更新的比较频繁,博客园这里用的redux版本是1.0.1,如果你关心最新版本的使用技巧,欢迎来我的Github查看(https://github.com/matthew-sun/blog/issues/18) ,我会在这里进行持续的更新和纠错. React是最好的前端库,因为其发源于世界上最好的后端语言框架. ---信仰 4.0 will likely be the last major release. Use Redux

ngRx 官方示例分析 - 5. components

组件通过标准的 Input 和 Output 进行操作,并不直接访问 store. /app/components/book-authors.ts import { Component, Input } from '@angular/core'; import { Book } from '../models/book'; @Component({ selector: 'bc-book-authors', template: ` <h5 md-subheader>Written By:<

使用Redux管理你的React应用

React是最好的前端库,因为其发源于世界上最好的后端语言框架. ---信仰 4.0 will likely be the last major release. Use Redux instead. It's really great. —Flummox框架作者 acdliteAndrew Clark 为什么使用React还需要使用别的框架来搭配? React的核心是使用组件定义界面的表现,是一个View层的前端库,那么在使用React的时候我们通常还需要一套机制去管理组件与组件之间,组件与数

React &amp; Redux 的一些基本知识点

一.React.createClass 跟 React.Component 的区别在于后者使用了ES6的语法,用constructor构造器来构造默认的属性和状态. 1. React.createClass import React from 'react';       const Contacts = React.createClass({             render() {                return (                    <div><

关于React的Container&amp;Presentational Component模型结构分析

react.js javascript 3 之前翻译了两篇关于Container&Presentational Component模型的文章,一篇是基础的Container和Component的定义,另外一篇是进阶版,因为翻译的太烂,感觉有很多错误,所以只放原文链接. 在这里我想讨论一下我自己对这个模型的一些想法. 注:便于书写,下面统一把Container&Presentational Components模型翻译为容器&展示组件模型 注:下面图片中的components文件夹指