How to use React refs to focus a Redux Form field?

前端 未结 2 829
独厮守ぢ
独厮守ぢ 2020-12-11 17:12

I am trying to use React refs to focus a Redux-Form Field when it mounts.

When I try this.refs.title.getRenderedComponent().focus() in componentD

2条回答
  •  我在风中等你
    2020-12-11 17:34

    Please try setting ref using callback function:

    ref={(input) => { this.title = input; }}
    

    and then use this to get underlying DOM node:

    ReactDOM.findDOMNode(this.title).focus();
    

    of if DOM input element is wrapped in another element:

    ReactDOM.findDOMNode(this.title).getElementsByTagName("input")[0].focus()
    

    According to React docs using refs with a string have some issues. Please check docs for more details.

提交回复
热议问题