连接 React component 和 Redux store

首先安装 react-redux,命令如下:

yarn add react-redux

连接 List 组件和 Redux store 的 sample code 如下:

import React from "react";
import { connect } from "react-redux";

const List = (props) => (
  <div>
    <h1>My List</h1>
    {props.location.city}
  </div>
);
const ConnectedList = connect((state) => {
  return {   // return things from state
    // the info only needed by List, no need to be 
    // everything from state!
    location: state.location   
  };
})(List);  // List: a react component to be connected to

export default ConnectedList;

connect()()形式比较特殊,返回一个 object,此 object 将以 props 传给组件 List。

上一篇:Failed to connect to raw.githubusercontent.com port 443: Connection refused


下一篇:connect函数以及未决连接队列