question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Focus an input after an action is dispatched

See original GitHub issue

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 the focus, so that’s not a good solution.

  componentDidUpdate() {
    const { isComposing } = this.props

    if (isComposing) {
      this.refs.composer.focus()
    }
  }

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,output

Not 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 the ref, as it fires after render happens: http://jsbin.com/yiputo/1/edit?js,console,output (cleanest solution, I believe, despite the use of state)

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

31reactions
gaearoncommented, Jan 26, 2016

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: https://github.com/rackt/react-redux/pull/270#issuecomment-175217424. However I’d rather use them for DOM nodes than for instances. Best not to expose instances or use their methods.

0reactions
timramonecommented, Oct 31, 2019

const { wasComposing: isComposing } = prevProps maybe? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Focus input after redux dispatch finished - Stack Overflow
Lets write a middleware that returns a promise after completing the dispatch. const thenMiddleware = store => next => action => { return...
Read more >
[Solved]-Focus input after redux dispatch finished-Reactjs
I faced a similiar problem, wherein, I had a list of items and I need to focus on the last element whenever a...
Read more >
<input type="search"> - HTML: HyperText Markup Language
An input in error state with a red focus ring. The user has entered the At this point, let's look at some useful...
Read more >
Forms in HTML documents - W3C
Control types created with INPUT; Examples of forms containing INPUT controls. The BUTTON element; The SELECT , OPTGROUP , and OPTION elements.
Read more >
HTML Forms - W3Schools
The user input is most often sent to a server for processing. ... the screen-reader will read out loud the label when the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found