Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autofocus react textarea?

I already try autoFocus or focus(), but seems not work.

<textarea autoFocus />

or

<textarea ref="textarea" />
componentDidMount() {
  findDOMNode(this.refs.textarea).focus();
}

edit

autoFocus seems work in chrome.

Both autoFocus foucs() not work in iOS.

like image 980
Liu Dongyu Avatar asked Jul 05 '26 07:07

Liu Dongyu


1 Answers

Calling focus() in componentDidMount seems to work in desktop Chrome and Firefox.

Demo.

It seems that Mobile Safari intentionally suppresses auto-focussing of elements.

like image 87
Jonathan Avatar answered Jul 07 '26 01:07

Jonathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!