[英]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.