Provider 内的任何一个组件,如果需要使用 state 中的数据,就必须是被 connect 过的组件——使用 connect 方法对你编写的组件进行包装后的产物。

class MyComp extends Component {
  // content...
}

const Comp = connect(...args)(MyComp);

connect() 接收四个参数,它们分别是 mapStateToPropsmapDispatchToPropsmergePropsoptions

mapStateToProps:

这个函数允许我们将 store 中的数据作为 props 绑定到组件上。

const mapStateToProps = (state) => {
  return {
    count: state.count
  }
}

 

相关文章:

  • 2022-12-23
  • 2023-03-15
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2023-01-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案