react-redux-v6: withRef 被移除。要访问包装的实例,请在连接的组件上使用 ref
Posted
技术标签:
【中文标题】react-redux-v6: withRef 被移除。要访问包装的实例,请在连接的组件上使用 ref【英文标题】:react-redux-v6: withRef is removed. To access the wrapped instance, use a ref on the connected component 【发布时间】: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?
【问题讨论】:
【参考方案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);
【讨论】:
以上是关于react-redux-v6: withRef 被移除。要访问包装的实例,请在连接的组件上使用 ref的主要内容,如果未能解决你的问题,请参考以下文章