[英]React Native TextInput ReRenders when calling useState Hook inside a custom component
Description When a TextInput component triggers action such as in onChangeText or onKeyPress method which then triggers setState, the component will re render and lose focuse.描述当 TextInput 组件触发 onChangeText 或 onKeyPress 方法中的操作然后触发 setState 时,组件将重新渲染并失去焦点。
React Native version: 0.62 (Unable to upgrade due to use of Expo) React Native 版本: 0.62(由于使用 Expo 无法升级)
Steps To Reproduce Provide a detailed list of steps that reproduce the issue.重现问题的步骤 提供重现问题的详细步骤列表。
Expected Results sets State but does not lose focus or rerenders预期结果集 State 但不会失去焦点或重新渲染
Snack, code example, screenshot, or link to a repository: Expo Example https://snack.expo.io/@ksi9302/1f9369小吃、代码示例、屏幕截图或存储库链接: Expo 示例https://snack.expo.io/69302/1f9
Hi Guys, this is a bug report I made to React Native.大家好,这是我向 React Native 提交的错误报告。 But I'm not sure if I'm doing something wrong here.
但我不确定我是否在这里做错了什么。
What I've tried so far and doesn't work到目前为止我已经尝试过但不起作用
What I know will work我所知道的会起作用
Bad Compromise糟糕的妥协
Take the wrapper out as it keeps getting rerendered due to the search value changes.取出包装器,因为它会由于搜索值的变化而不断重新渲染。
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.