question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Focusing on a text input when navigating to a new screen only works when adding to the stack, not when going back.

See original GitHub issue

Current Behavior

Calling .focus() on a text input (or using autoFocus) when the screen becomes focused works when you add a screen to the stack (Screen A -> Screen B) but not when going back (Screen B -> Screen A). Instead, the input is focused for a second then blurs immediately.

cannot focus

Expected Behavior

The input should stay focused

How to reproduce

  • Navigate back to a screen that has a text input with autoFocus on true
  • OR call .focus() on the reference of the text input when the screen is focused
  • OR copy-paste the following code:
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;

Your Environment

    "@expo/vector-icons": "^10.0.0",
    "@react-native-community/masked-view": "0.1.6",
    "@react-navigation/bottom-tabs": "^5.2.5",
    "@react-navigation/material-bottom-tabs": "^5.1.7",
    "@react-navigation/native": "^5.1.4",
    "@react-navigation/stack": "^5.2.9",
    "@react-navigation/web": "^1.0.0-alpha.9",
    "@reduxjs/toolkit": "^1.2.5",
    "expo": "^37.0.0",
    "expo-asset": "~8.1.3",
    "expo-constants": "~9.0.0",
    "expo-font": "~8.1.0",
    "expo-web-browser": "~8.1.0",
    "moment": "^2.24.0",
    "react": "16.9.0",
    "react-dom": "16.9.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-37.0.0.tar.gz",
    "react-native-gesture-handler": "~1.6.0",
    "react-native-paper": "^3.6.0",
    "react-native-remote-svg": "^2.0.6",
    "react-native-safe-area-context": "0.7.3",
    "react-native-screens": "~2.2.0",
    "react-native-svg": "11.0.1",
    "react-native-web": "^0.11.7",
    "react-native-webview": "8.1.1",
    "react-redux": "^7.1.3",
    "redux": "^4.0.5",
    "tinycolor2": "^1.4.1"

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Luan-Senscommented, Apr 6, 2020

+1

2reactions
divonelnccommented, Jul 10, 2020

I just submitted this bug to react-navigation: https://github.com/react-navigation/react-navigation/issues/8564 My bad, I should have started with that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Focusing on a text input when navigating to a new screen only ...
Focusing on a text input when navigating to a new screen only works when adding to the stack, not when going back. #8564....
Read more >
React-navigation focus on input blurs immediately when going ...
This works when I add a screen to the stack, but not when I go back in the stack. Instead, the input focuses...
Read more >
Call a function when focused screen changes | React Navigation
React Navigation provides a hook that returns a boolean indicating whether the screen is focused or not. The hook will return true when...
Read more >
Navigate to a destination - Android Developers
Navigating to a destination is done using a NavController , an object that manages app navigation within a NavHost .
Read more >
SetFocus function in Power Apps - Microsoft Learn
a screen is displayed, to focus the first input control with the OnVisible property of the Screen. The control with focus may be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found