![](/img/trans.png)
[英]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.