javascript react-redux-v6:删除了 withRef。要访问包装的实例,请在连接的组件上使用 ref
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53819335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
react-redux-v6: withRef is removed. To access the wrapped instance, use a ref on the connected component
提问by 2 8
I want to call a function from a connected component using ref, so I used before from withRef: truein connected component:
我想使用从连接组件调用一个函数ref,所以我之前withRef: true在连接组件中使用过:
export default connect(
mapStateToProps, mapDispatchToProps, null, {withRef: true}
)(InviteReceiverForm)
and in the presentational component:
并在展示组件中:
<ExampleComponent
ref={ cmp => { if(cmp) { this.individualSenderFormRef = cmp.getWrappedInstance() }} />
But after I updated to react-redux v6, I got this error:
但是在我更新到之后react-redux v6,我收到了这个错误:
withRef is removed. To access the wrapped instance, use a ref on the connected component
withRef is removed. To access the wrapped instance, use a ref on the connected component
How can I use ref in react-redux v6?
我如何使用 ref in react-redux v6?
回答by Ian Kemp
https://github.com/reduxjs/react-redux/releases/tag/v6.0.0
https://github.com/reduxjs/react-redux/releases/tag/v6.0.0
The
withRefoption to connect has been replaced withforwardRef. If{forwardRef : true}has been passed toconnect, adding a ref to the connected wrapper component will actually return the instance of the wrapped component.
该
withRef选项连接已被替换forwardRef。如果{forwardRef : true}已传递给connect,则向连接的包装组件添加 ref 将实际返回包装组件的实例。
回答by Tu Duong
It's work for me:
这对我有用:
connect(
mapStateToProps,
null,
null,
{
forwardRef: true
}
)
)(ComponentName);

