-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Can I safely use Input.prototype.inputRef? #1980
Comments
EDIT Fixed code snippet 😊 Only public APIs are tested and versioned. There is no telling what we may do with the private methods and members between releases :) I'd strongly encourage avoiding accessing them. The parts of the DOM for which React doesn't provide a virtual DOM abstraction (focus, setSelectionRange, etc) it requried to reach out to the native DOM, unfortunately. I'd suggest doing something simple, such as: <Input id="my-input" />
const input = document.querySelector('#my-input')
input.setSelectionRange() The Someday JS will have truly private fields, but not quite yet. |
What to do if I want to make util input component or mix some behaviour? Does make React provides interface InputProps {
inputRef: (input: HTMLInputElement | null) => void
} It looks "natural" and obviously for me as React developer. Because I used to use Also it will be work for <Form.Field
inputRef={(input: HTMLInputElement | null) => this.input = input}
/> vs <Form.Field
control={
props =>
<Input ref={(input: any) => {
this.input = input ? findDomNode(input).querySelector('input') : null;
}
} />
}
/> vs componentDidMount() {
this.input = findDomNode(this).querySelector('.some-hard-code-class-or-id input');
}
render() {
return <Form.Field className="some-hard-code-class-or-id" />
} In handleInputRef = c => {
this.inputRef = c;
if (typeof this.props.inputRef === 'function') {
this.props.inputRef(c);
}
} (and other stuff for omitting This way is present in many other React libs (like React Bootstrap). |
I could make a PR if I have a chance to persuade you 😄 |
#1879 will introduce |
Hm. <Form.Field
control={props =>
<Input {...props} innerRef={(input: HTMLInputElement) => this.input = input} />
}
/> ? UPD: Hm.. As I understand it will be: <Form.Field innerRef={(input: HTMLInputElement) => void} />
<Input
innerRef={(input: HTMLInputElement) => void}
ref={(input: React.ReactInstance) => void}
/>
// if I want to get InputInstance from Form.Field I should use control prop:
<Form.Field
control={props => <Input {...props} ref=(inst: React.ReactInstance) => void }
/> |
<Form.Field innerRef={(div: HTMLDividerElement) => void} />
<Form.Input innerRef={(div: HTMLDividerElement) => void} /> Because, |
Hello!
I need to get a DOM node from
Input
component. Not for focus only. For example I need to callinput.setSelectionRange
.So. I have not found how to get DOM node in the documentation. But I have found
inputRef
in the code. May be it will be worth to add this property as "public" API in the documentation and TS definitions (like in this #1970)?At the moment I can get input element only with
findDOMNode
that is not a goodReact
practice.The text was updated successfully, but these errors were encountered: