-
-
Notifications
You must be signed in to change notification settings - Fork 3.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
Question: Focus an input after an action is dispatched #266
Comments
Looks like render() {
...<Composer ref={c => this._composer = c; c && c.focus()} />
}
onCompose() {
//... dispatch action
this._composer && this._composer.focus() // for cases Composer is already visible and button clicked
} |
I'd do something like componentDidUpdate(prevProps) {
const { isComposing } = this.props
const { isComposing: wasComposing } = prevProps
if (!wasComposing && isComposing) {
this.refs.composer.focus()
}
} I agree callback refs are nice: #270 (comment). However I'd rather use them for DOM nodes than for instances. Best not to expose instances or use their methods. |
const { isComposing: wasComposing } = prevProps Could you please Explain the above line... |
@bvmCoder : not the best place to ask that question, but I'll go ahead and answer. That is ES6 "destructuring assignment". It's taking the For more info, see these links on ES6 syntax and features. |
|
I have a button that dispatches an action that toggles a boolean in the store. As a result, an input is rendered in the component. I want to focus on that input after the button is clicked, but I can't do it immediately because the
this.refs
is not updated until the new props are received.If this is handled in
componentDidUpdate
, all subsequent action will keep triggering thefocus
, so that's not a good solution.Example: http://jsbin.com/nerexi/2/edit?js,console,output
Another potential solution is a combination of focusing from the
componentDidMount
and also from the button callback: http://jsbin.com/viwive/3/edit?js,console,outputNot a big fan of this as it seems like the container component's job to focus.
However, using
setState
and its callback actually allows to access theref
, as it fires after render happens: http://jsbin.com/yiputo/1/edit?js,console,output (cleanest solution, I believe, despite the use ofstate
)This question relates to any UI forced interaction that needs to happen after an action but can't be in
render
. Another example would be scrolling to an element to bottom after an action is dispatched.The text was updated successfully, but these errors were encountered: