簡體   English   中英

如何專注於第一個輸入字段 - redux-form

[英]How to focus on first input field - redux-form

我正在嘗試以任何形式設置第一個輸入字段以進行聚焦。 那么,實現這樣的事情的最佳做法是什么? 我怎么能檢查字段的類型? 是文本,數字,復選框等?

是通過聽@redux-form動作,然后發出焦點動作?

任何人都可以提出解決方案嗎?

使用此方法,您可以每次配置autoFocus

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

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

}

我認為對於redux-form (v6 +),當前的技術狀態是為你想要自動聚焦的<input />提供autoFocus屬性。

例:

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

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

}

這是Github問題鏈接

您當然可以將autoFocus prop作為field.input鍵提供。

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

如果你想使用無狀態功能組件,refs對你沒有好處。 我的解決方案是將第一個= {true}道具傳遞給Field並在組件內部檢查它。 它存在然后在輸入字段上設置autoFocus。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM