【问题标题】:react-redux-v6: withRef is removed. To access the wrapped instance, use a ref on the connected componentreact-redux-v6: withRef 被移除。要访问包装的实例,请在连接的组件上使用 ref
【发布时间】:2019-05-18 01:30:16
【问题描述】:

我想使用ref从连接组件调用函数,所以我之前在连接组件中使用了withRef: true

export default connect(
  mapStateToProps, mapDispatchToProps, null, {withRef: true}
)(InviteReceiverForm)

在演示组件中:

<ExampleComponent 
  ref={ cmp => { if(cmp) { this.individualSenderFormRef = cmp.getWrappedInstance() }} />

但是在我更新到react-redux v6之后,我得到了这个错误:

withRef is removed. To access the wrapped instance, use a ref on the connected component

如何在react-redux v6 中使用 ref?

【问题讨论】:

    标签: javascript reactjs redux react-redux


    【解决方案1】:

    https://github.com/reduxjs/react-redux/releases/tag/v6.0.0

    withRef 连接选项已替换为 forwardRef。如果{forwardRef : true} 已传递给connect,则向连接的包装组件添加引用实际上将返回被包装组件的实例。

    【讨论】:

      【解决方案2】:

      这对我有用:

      connect(
          mapStateToProps,
          null,
          null,
          {
            forwardRef: true
          }
        )
      )(ComponentName);
      

      【讨论】:

        猜你喜欢
        • 2019-05-10
        • 2018-01-03
        • 2020-02-26
        • 1970-01-01
        • 2023-03-30
        • 2020-04-28
        • 2018-11-18
        • 2019-09-05
        • 2020-04-30
        相关资源
        最近更新 更多