1

How to add autofocus property in LI element in react? I've used through ref but is not working.

render() {
  return (
    <div>
      <li ref={(re) => re.focus()}>aaa</li>
    </div>
  )
}
Shubham
  • 1,163
  • 2
  • 12
  • 36
  • Possible duplicate of [React set focus on input after render](https://stackoverflow.com/questions/28889826/react-set-focus-on-input-after-render) – falinsky Jun 14 '18 at 15:07

2 Answers2

1

Try using componentDidMount and focusing the ref inside that function.

Also, like Tomasz said, an li element is not focusable, so you would need to use an input, button, or anchor-type element to be able to focus.

Example: React set focus on input after render

John
  • 705
  • 3
  • 9
  • 18
-1

Focus works only on controls like input, button, anchors. You can't do it on list elements, divs etc. Maybe just make it a button? Clickable elements should be buttons / anchors.

Tomasz Nowak
  • 175
  • 2
  • 10
  • Not really. List elements, divs etc can be focusable. You can read more about `tabindex` html attribute: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex – falinsky Jun 14 '18 at 15:06