【问题标题】:How to access props in stateless component that were mapped in mapStateToProps如何访问在 mapStateToProps 中映射的无状态组件中的道具
【发布时间】:2018-09-01 20:42:25
【问题描述】:

我想知道如何访问未传递但在 mapStateToProps 函数中映射到无状态组件中的道具的道具。因此,例如,如果我将 state 中的值映射到 prop 如下:

const mapStateToProps = state => ({
  test: formValueSelector('InfoPanel')(state, 'test'),
});

如果我期望像这样来自父组件的一些道具:

export const InfoPanelForm = ({ readOnly, hasActionPoints}) => (
  <div>
    <Row>
      <Column>
        <VerticalSpacer space={4} />
        <Element>Info</Element>
      </Column>
    </Row>
  </div>

我现在如何在这个组件中访问这个属性测试?

【问题讨论】:

  • mapStateToProps 只会传递地图道具。这是将参数和函数传递给类或无状态组件的方式
  • @RIYAJKHAN 是的,但我问我现在如何访问这个道具,在我的情况下进行测试?
  • @Leff 请检查以下答案广告,如有任何问题请添加评论

标签: reactjs react-redux


【解决方案1】:

您可以像使用readOnly 一样访问。

假设您使用给定组件 InfoPanelFormmapStateToProps 调用了 connect

connect(mapStateToProps)(InfoPanelForm); //react-redux connect call

组件:

export const InfoPanelForm = ({ readOnly, hasActionPoints, test}) => (
  <div>
    //..
  </div>

【讨论】:

    猜你喜欢
    • 2020-07-15
    • 2017-06-08
    • 2018-07-06
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多