簡體   English   中英

我該如何重新聚焦<textinput>事件之后?</textinput>

[英]How do I refocus a <TextInput> after an event?

當屏幕加載時,我使用以下代碼來關注一個:

useEffect(() => {
  InteractionManager.runAfterInteractions(() => {
    if (refText.current) {
      refText.current.focus();
    }
  });
}, []);

這按預期工作。 但是,在提交事件上,需要再次關注。

function myEvent() {
  if (refText.current) {
    refText.current.focus();
  }
}

此處不再關注正文。 有任何想法嗎?

實際上,我不知道為什么您需要在提交處理程序中使用if語句來將焦點設置在TextInput上,但無論如何,我假設您的Button聲明中有一個事件處理程序。 例如:

function App() {
  const textRef = useRef();

  const handleSubmit = () => {
    if (textRef.current) textRef.current.focus();
  };

  return (
    <View>
      <TextInput placeholder="Input" ref={textRef}/>
      <Button title="Submit" onPress={handleSubmit} />
    </View>
  );
}
});

正如您在這個Sandbox中看到的那樣,它工作正常。

暫無
暫無

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

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