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);
  • 相关阅读:
    MP4文件格式
    ASP.net 学习路线(详细)
    H.264开源解码器评测
    H264编码 封装成MP4格式 视频流 RTP封包
    MP4介绍与基本AVC编码(x264)教程
    创建一个简单的WCF程序
    VUE 从零开始 学习笔记 一
    关于阿里云短信接口的使用
    对Ul下的li标签执行点击事件——如何获取你所点击的标签
    二逼程序员与苦逼程序员
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5185401.html
Copyright © 2011-2022 走看看