Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace string ref usage with callback refs #170

Closed
wants to merge 4 commits into from

Conversation

ablamunits
Copy link

Hi,

Setting element refs using "String Refs" is considered legacy API by React and is generally discouraged due to certain issues.

We personally ran into issues in our own setup, where the usage of string refs caused the following error to appear when ReactTelephoneInput was mounted in a our larger application, bundled with browserfiy:

removeComponentAsRefFrom(...): Only a ReactOwner can have refs. You might be removing a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded (details: https://fb.me/react-refs-must-have-owner).

I did not dig any deeper into the exact root cause of this, and perhaps this has something to do with the structure of our project. However, using callback refs in telephone input has solved the issue for us and it seems to be a reasonable solution, considering the currently used API will be deprecated at some point.

Thanks

// If formatted number has changed, call on change
if (this.state.formattedNumber !== prevFormattedNumber) {
this.props.onChange(this.state.formattedNumber, this.state.selectedCountry);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this component maintains its own state, if there are changes to the value of the telephone field that were caused by the incoming props these changes will be applied in _mapPropsToState. Prior to this fix, such a change in the state will not call onChange. This fixes it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukeshsoni any objections to this change? Perhaps this was suggested in the past?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

componentWillReceiveProps is doing a lot in this component. I shouldn't have done this in the first place. This piece of code has been the source of a lot of bugs in this component. I am thinking about completely removing componentWillReceiveProps and give an alternate solution making it a controlled/uncontrolled/(controlled + uncontrolled) component

@mukeshsoni
Copy link
Owner

@ablamunits I have completely removed the dependency on react-dom and also removed all the refs on all the list items. Have switched to using react-tiny-virtual-list, which implements windowing on the list and hence is much more performant. Win-win.

@mukeshsoni mukeshsoni closed this May 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants