-
Notifications
You must be signed in to change notification settings - Fork 11
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
How to deal with using refs in pure functional components? #18
Comments
In that particular case I'd be more inclined to use the URL hash to move to the bottom, rather than manipulating DOM elements directly — maybe even calling it However you do make a good point. I admit there will be a handful of cases with which you'll want to manipulate DOM elements through Thoughts? |
I've actually been missing it a bit. For simple things like resetting inputs when calling an action. Sometimes more plumbing to keep track of those inside some reducer state. But maybe I'm just used to being able to access the refs so finding another solution for those times seems wonky now at least |
Ideally you should model the entire form in Redux, and so resetting the form would be one simple action to empty the values, these empty values are then propagated through the components and the inputs are reset. Using refs to directly manipulate DOM elements is strongly discouraged in React – you should be either passing them through as props — which is the Keo approach — or maintain a local component state using |
Ahh yes in the perfect world :) But we are creating standalone components that should work within redux, angular etc... So our own implementation uses redux and co but we can't have that as a dependency for the components to work. But yes you are right |
@eisisig I share your pain! We have lots of horrific legacy code at work that needs to be dealt with and refactored accordingly. |
Don't get me started ;) But I think the pureness that keo brings and the more functional approach outweight the missing refs. So great job |
What about using Function refs instead of String refs? String refs will eventually be removed from React anyway, I didn't even implement them in preact since they seem ill-fated. |
@developit can you give an example on functional refs |
class Foo {
render() {
return (
<div>
<OtherComponent ref={ c => this.otherComponent = c } />
</div>
);
}
} Working example: Here's a little helper function that emulates the behavior of string refs using function refs: https://gist.github.com/developit/63e7a81a507c368f7fc0898076f64d8d |
One common use case for ref that I still run across is the need to run public component methods on some third-party components. While I'd prefer authors not create these kinds of APIs in the first place, they're still somewhat ubiquitous and all assume the use of ref as a way to hook into the API via the smart component parent. See for example: caseywebdev/react-list#90 |
Say for example I have a need for refs when building a chat app and expecting the chat box to scroll down when new messages arrive.
The text was updated successfully, but these errors were encountered: