[英]useAsyncStorage custom hook with useState hook in React Native
[英]React Native TextInput ReRenders when calling useState Hook inside a custom component
描述當 TextInput 組件觸發 onChangeText 或 onKeyPress 方法中的操作然后觸發 setState 時,組件將重新渲染並失去焦點。
React Native 版本: 0.62(由於使用 Expo 無法升級)
重現問題的步驟 提供重現問題的詳細步驟列表。
預期結果集 State 但不會失去焦點或重新渲染
小吃、代碼示例、屏幕截圖或存儲庫鏈接: Expo 示例https://snack.expo.io/69302/1f9
大家好,這是我向 React Native 提交的錯誤報告。 但我不確定我是否在這里做錯了什么。
到目前為止我已經嘗試過但不起作用
我所知道的會起作用
糟糕的妥協
取出包裝器,因為它會由於搜索值的變化而不斷重新渲染。
import React, { useState } from "react";
import { View, TextInput } from "react-native";
const Test = () => {
const handleChange = e => {
setSearch(e);
};
const [search, setSearch] = useState(null);
return (
<Wrapper>
<TextInput
value={search}
onChangeText={e => {
handleChange(e);
}}
placeholder="type here"
/>
</Wrapper>
);
};
const Wrapper = props => {
return (
<View
style={{
width: "100%",
height: "100%",
alignItems: "center",
justifyContent: "center",
display: "flex",
}}
>
{props.children}
</View>
);
};
export default Test;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.