当回到上一屏幕时,输入的React-navigation焦点立即模糊。

问题描述 投票:1回答:1

当我导航到一个新的屏幕时,我想关注一个文本输入。当我把一个屏幕添加到堆栈中时,这样做是可行的,但当我回到堆栈中时就不行了。

相反,输入的焦点有一秒钟,然后立即模糊。

这是我得到的结果。

  • 屏幕A是堆栈中的第一个,输入立即模糊了
  • 画面B被添加到堆栈中,并按原计划工作

enter image description here

知道是什么原因吗?

顺便说一下,如果我使用autoFocus,我也有同样的问题。

下面是整个(相当直接的)代码。

import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import * as React from "react";
import { TextInput, View, Button, Text } from "react-native";

function ScreenA({ navigation }) {
    const textInputRef = React.useRef();

    const focusOnInput = e => {
        textInputRef.current.focus();
    };

    navigation.addListener("focus", focusOnInput);
    return (
        <View>
            <Text>SCREEN A</Text>
            <TextInput ref={textInputRef} />
            <Button title="Go to screen B" onPress={() => navigation.navigate("ScreenB")} />
        </View>
    );
}

function ScreenB({ navigation }) {
    const textInputRef = React.useRef();

    const focusOnInput = e => {
        textInputRef.current.focus();
    };

    navigation.addListener("focus", focusOnInput);

    return (
        <View>
            <Text>SCREEN B</Text>
            <TextInput ref={textInputRef} />
            <Button title="Go to screen A" onPress={() => navigation.navigate("ScreenA")} />
        </View>
    );
}

const Stack = createStackNavigator();

function TestComp() {
    return (
        <NavigationContainer>
            <Stack.Navigator>
                <Stack.Screen component={ScreenA} name="ScreenA" />
                <Stack.Screen component={ScreenB} name="ScreenB" />
            </Stack.Navigator>
        </NavigationContainer>
    );
}

export default TestComp;
react-navigation react-native-textinput
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.