简体   繁体   English

在自定义组件中调用 useState Hook 时反应本机 TextInput ReRenders

[英]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.重现问题的步骤 提供重现问题的详细步骤列表。

  1. Create a Custom Wrapper Component simple like创建一个简单的自定义包装器组件,例如
  2. Declare useStateHook声明 useStateHook
  3. Pass TextInput to the Wrapper Component either by direct JSX or Custom Component通过直接 JSX 或自定义组件将 TextInput 传递给 Wrapper 组件
  4. Bind setState function to any of event listeners of TextInput.将 setState function 绑定到 TextInput 的任何事件侦听器。

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到目前为止我已经尝试过但不起作用

  1. Get rid of all styles.摆脱所有 styles。
  2. make custom input component with class react component, disable shouldComponentUpdate使用 class 反应组件制作自定义输入组件,禁用 shouldComponentUpdate
  3. not binding value不绑定值
  4. make different state structure and actually pass within object {}制作不同的 state 结构并在 object {}
  5. make dummy key制作虚拟钥匙

What I know will work我所知道的会起作用

  • Get rid of custom wrapper and use plain JSX (In other words, not passing TextInput as children component) //Almost impossible when app gets bigger摆脱自定义包装器并使用普通 JSX(换句话说,不将 TextInput 作为子组件传递)//当应用程序变大时几乎不可能

Bad Compromise糟糕的妥协

  • using AutoFocus={true} //on Web works fine, but on Mobile, keyboard flickers a lot.使用 AutoFocus={true} //on Web 工作正常,但在移动设备上,键盘闪烁很多。

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.

相关问题 在 React Native 中使用 useState 钩子的 useAsyncStorage 自定义钩子 - useAsyncStorage custom hook with useState hook in React Native 在自定义组件的视图中使用 TextInput 时不可交互(React-Native) - TextInput is not interactable when being used inside a view in a custom component (React-Native) useState 在自定义反应挂钩中不起作用 - useState not working inside a custom react hook 当组件在 React Native 中重新渲染时,动态不透明度不会改变 - Dynamic Opacity not changing when component rerenders in react native React Native 无法识别 useState 钩子 - React Native not recognizing useState hook 在另一个组件内部使用时,React 自定义钩子 setValue 未定义 - React custom hook setValue is undefined when used inside another component 如何在 React 中找到调用自定义 hook 的组件? - How to find the component calling a custom hook in React? React Hook &quot;useState&quot; 在函数 &quot;setResults&quot; 中调用,它既不是 React 函数组件,也不是自定义 React Hook 函数 - React Hook "useState" is called in function "setResults" which is neither a React function component or a custom React Hook function React Hook“useState”在 function“increaseCounter”中被调用,它既不是 React function 组件,也不是自定义 React Hook function - React Hook "useState" is called in function "increaseCounter" that is neither a React function component nor a custom React Hook function 使用 useState 钩子时反应组件渲染两次 - React component rendering twice when using useState hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM