-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Referencing issue #172
Comments
Not to be confused with the render() function that you define on your component (and which returns a virtual DOM element), ReactDOM.render() will return a reference to your component's backing instance (or null for stateless components). So they will not work for stateless components. Ref's are not usually needed but the use case especially for inputs is surely there. |
Actually one more note: It would be quite simple to add support for them to stateless components also without making them have state. |
I'm guessing that you want the If that is the case, save the trouble and don't use As you mentioned, ref's are not usually needed... But there probably should be a way to get a ref (for other reason)... Most of the components are stateless, which poses an issue. The comment in the react repo with an example is mostly showing how to get a ref for children DOM, plus the whole "getRef" style prop seems dirty. @eddywashere would have to chime in if there should be a way to access refs and, if so, which way (via "prop-in-the-middle" or make components not stateless). |
Good discussion! Thanks @TheSharpieOne for your feedback. I was going to ping you here to see what you thought :) I've been less inclined to hook into a ref for focus just over personal preference. Besides Input, do other components need ref exposed for them? I think an input is the unique example that breaks down the stateless functional component. The react docs for "refs to component" use the ref callback to set focus on an input. There's probably a few more use cases for it. That alone is worth switching this over to a normal component instead of the prop in the middle. The prop in the middle is a weird thing to document and encourage. I'm hesitant to switch the other functional components over. If there's a reasonable need, like input has, feel free to discuss them here or open a separate issue to track the discussion/request. Does that work for you @madisvain? |
Released 3.4.1 to enable refs on Input. Closing this issue out. @madisvain if there are other components that should be switched over, please create a separate issue for them with a use case. Thanks! |
Can you provide a demo code to the doc? (reactstrap v3.7.0) I am using the ref in the input component, however, I only can get the react element, but not the input DOM. Would you expose the input DOM? <Input
type="text"
name="username"
ref={(input) => (this.username = input)}
id="username"
defaultValue='root' />
|
@CrisLi thanks for pinging me. Reopening this issue. I tried putting together a demo and came across this warning: https://facebook.github.io/react/warnings/special-props.html Looks like a prop like |
transferring from @ajainarayanan in #218 (comment)
|
@ajainarayanan Yeah, I couldn't find a good reason to need a ref without doing something that could be done in a more explicit react way. Focus was the only thing I could think of (and is the only example the react docs has for them). |
^ +1. Closing this out as example: <Input
type="text"
name="username"
getRef={(input) => (this.username = input)}
id="username"
defaultValue='root' /> |
ok , the getRef is work fine,thinks |
for anyone that comes across this, looks like the prop has been changed from getRef to innerRef: https://github.com/reactstrap/reactstrap/blob/5.0.0-alpha.3/src/Input.js#L15 |
How would I access methods like |
@Hum4n01d by providing the onChange prop. |
Interesting, I assumed that since it wasn't listed as a prop that it wouldn't work. Looks like my code is having an unrelated problem. Thanks!
On Nov 10, 2017, at 03:09 AM, Evan Sharp <[email protected]> wrote:
@Hum4n01d by providing the onChange prop.
Every prop the component receives is passed down, but ref is special as react itself consumes it and doesn't pass it down. That is why there is a special case for ref.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
this is very unclear to other devs using reactstrap, I was lucky to find this by google or I would never have known, unless I started reading through the source code (not clear this dependency is breaking refs btw) also refs are required with some third party libraries so it should be of some importance to not break a core feature of the framework you build a library for. innerRef should be changes back to ref and properly go to the input component. Please let me know why there would be any objection to this. |
@Kielan reactstrap doesn't have that ability, that is core react. ReactJS defines ref and how it is used. React does NOT pass ref down, it consumes it.
Ref: https://reactjs.org/docs/refs-and-the-dom.html#exposing-dom-refs-to-parent-components |
Using refs for components. I have not tested other components but it seems using refs on for example Input component does not work and the input is not made available. If I replace this with a regular input then everything works as expected.
https://facebook.github.io/react/docs/more-about-refs.html
I used the input like this:
With the datepicker overylay example from here:
http://react-day-picker.js.org/examples/?overlay
The text was updated successfully, but these errors were encountered: