1

当我导航到新屏幕时,我想专注于文本输入。这在我将屏幕添加到堆栈时有效,但当我返回堆栈时无效。

相反,输入聚焦一秒钟并立即模糊。

这是我得到的:

  • 屏幕A在堆栈中是第一个并且立即输入模糊
  • 屏幕 B 被添加到堆栈中并按预期工作

在此处输入图像描述

知道是什么原因造成的吗?

仅供参考,如果我使用自动对焦,我也会遇到同样的问题。

这是整个(非常简单的)代码:

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;
4

0 回答 0