Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-redux-v6: withRef is removed. To access the wrapped instance, use a ref on the connected component

I want to call a function from a connected component using ref, so I used before from withRef: true in connected component:

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:

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

How can I use ref in react-redux v6?

like image 526
Vahid Kharazi Avatar asked Dec 17 '18 16:12

Vahid Kharazi


2 Answers

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

The withRef option to connect has been replaced with forwardRef. If {forwardRef : true} has been passed to connect, adding a ref to the connected wrapper component will actually return the instance of the wrapped component.

like image 147
Ian Kemp Avatar answered Oct 21 '22 15:10

Ian Kemp


This worked for me:

connect(
    mapStateToProps,
    null,
    null,
    {
      forwardRef: true
    }
  )
)(ComponentName);
like image 6
Tu Duong Avatar answered Oct 21 '22 17:10

Tu Duong