zoukankan      html  css  js  c++  java
  • [Redux] Generating Containers with connect() from React Redux (VisibleTodoList)

    Learn how to use the that comes with React Redux instead of the hand-rolled implementation from the previous lesson.

    Code to be refactored:

    class VisibleTodoList extends Component {
      componentDidMount() {
        const { store } = this.context;
        this.unsubscribe = store.subscribe(() =>
          this.forceUpdate()
        );
      }
      
      componentWillUnmount() {
        this.unsubscribe();
      }
      
      render() {
        const props = this.props;
        const { store } = this.context;
        const state = store.getState();
        
        return (
          <TodoList
            todos={
              getVisibleTodos(
                state.todos,
                state.visibilityFilter
              )
            }
            onTodoClick={id =>
              store.dispatch({
                type: 'TOGGLE_TODO',
                id
              })            
            }
          />
        );
      }
    }
    
    VisibleTodoList.contextTypes = {
      store: React.PropTypes.object
    };

    In the code, we handle the 'context', subscribe state and unsubscribe. Also we need to always remember write 'contextTypes'. There are lots of things todo.

    Actually we can use 'ReactRedux' libaray to simpy our life:

    'ReactRedux' libaray's connect() fucntion also pass the context down to the component. 

    VisibleTodoList:

    So get the todos and write into mapStateToProps function:

    const mapStateToProps = (state) => {
      return {
        todos: getVisibleTodos(
          state.todos,
          state.visibilityFilter
        )
      };
    };

    then get onTodoClick dispatch function to the mapDispatchToProps function:

    const mapDispatchToProps = (dispatch) => {
      return {
        onTodoClick: (id) => {
          dispatch({
            type: 'TOGGLE_TODO',
            id
          });
        }
      };
    };

    then we can use 'connect()()' function to connect state, dispatch action and Render component together:

    const { connect } = ReactRedux;
    const VisibleTodoList = connect(
      mapStateToProps,
      mapDispatchToProps
    )(TodoList);

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

    code:

    const mapStateToProps = (state) => {
      return {
        todos: getVisibleTodos(
          state.todos,
          state.visibilityFilter
        )
      };
    };
    
    const mapDispatchToProps = (dispatch) => {
      return {
        onTodoClick: (id) => {
          dispatch({
            type: 'TOGGLE_TODO',
            id
          });
        }
      };
    };
    
    const { connect } = ReactRedux;
    const VisibleTodoList = connect(
      mapStateToProps,
      mapDispatchToProps
    )(TodoList);
  • 相关阅读:
    AT24C0X I2C通信原理
    Windows文件夹、文件源代码对比工具--WinMerge
    SignalTap导致PCIe Read/Write卡死
    Windows CMD 支持ls命令
    何为内存模型(JMM)?
    何为内存重排序?
    何为安全发布,又何为安全初始化?
    Hibernate入门之many to many关系映射详解
    Hibernate入门之one to many关系映射详解
    Hibernate入门之one to one关系映射详解
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5185401.html
Copyright © 2011-2022 走看看