简体   繁体   中英

How to focus on first input field - redux-form

I'm trying to set the first input field in any form to be focused. So, what is the best practice to implement something like this? And how could I check the type of the field? is it text, number, checkbox or etc.?

Is it by listening to @redux-form actions, and then dispatch a focus action?

Can any one suggest a solution ?

Using this approach, you can configure your autoFocus every time.

<Field component={renderFirstInput} autoFocus={true/false} />

const renderFirstInput = ({ autoFocus }) => {
  return ( <input autoFocus={autoFocus} /> )

}

I think that for redux-form (v6+) the current state of art is to provide autoFocus property for the <input /> which you would like to be auto focused.

Example:

<Field component={renderFirstInput} type="text" />

const renderFirstInput = field => {
  return ( <input {...field.input} autoFocus /> )

}

Here is the Github issue link

You may of course provide autoFocus prop as a field.input key.

在React文档中有一个很好的例子: https//facebook.github.io/react/docs/refs-and-the-dom.html

If you want to use stateless functional components refs will do you no good. My solution is to pass a first={true} prop to Field and check for it inside the component. It it exists then set autoFocus on the input field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM