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.

text input focus programatically not working

See original GitHub issue

Current behaviour

I am trying to auto focus, when i click on edit. initially it will be disabled.

Expected behaviour

It should auto focus when i click on edit button. but it is not focusing.

Code sample

const bioRef = useRef(null);


<View style={styles.action}>
	<View style={styles.bioContainer}>
		<Text style={styles.field_title}>Write your bio...</Text>
		{disabled ? (
			<TouchableOpacity
				style={styles.editText}
				onPress={async() => {
					setPlaceholder("");
					await setDisabled(false);
					bioRef.current.focus();
				}}>
				<Text style={styles.editTextLbl}>Edit</Text>
			</TouchableOpacity>
		) : (
			<TouchableOpacity
				style={styles.editText}
				onPress={() => {
					setDisabled(true);
				}}>
				<Text style={styles.editTextLbl}>Save</Text>
			</TouchableOpacity>
		)}
	</View>
</View>
<View style={styles.action}>
	<TextInput
		ref = {bioRef}
		style={styles.textInput}
		multiline={true}
		disabled={disabled}
		numberOfLines={4}
		theme={input_theme}
		placeholderTextColor="#767676"
		keyboardType="default"
		autoCapitalize="none"
		placeholder={placeholder}
		value={bio}
		onChangeText={(text) => setBio(text)}
	/>
</View>

Screenshots (if applicable)

Your Environment

software version
android 11
react-native 0.64.2
react-native-paper 4.9.2
node v15.12.0
npm 7.20.0
expo sdk
react-native-vector-icons 7.1.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
Elabarcommented, Jul 19, 2021

This is because setState is an async operation. Which mean when the line bioRef.current.focus(); is run, the field is probably still in disabled state.

To fix this issue, you can use useEffect to watch your disabled state to focus the field like so.

const [disabled, setDisabled] = useState(false)

useEffect(() => {
   if(!disabled){
      bioRef.current?.focus();
   }
}, [disabled])

You can then remove bioRef.current.focus(); in your onPress handler.

Using useEffect can fix this issue is because the callback in useEffect will run AFTER the state is changed on UI.

0reactions
github-actions[bot]commented, Aug 19, 2021

Hello 👋, this issue has been open for more than a month without a repro or any activity. If the issue is still present in the latest version, please provide a repro or leave a comment within 7 days to keep it open, otherwise it will be closed automatically. If you found a solution or workaround for the issue, please comment here for others to find. If this issue is critical for you, please consider sending a pull request to fix it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jquery - $.focus() not working - Stack Overflow
I was using ng-repeat for some li elements inside a list and I was not able to bring focus to the first li...
Read more >
HTMLElement.focus() - Web APIs | MDN
The HTMLElement.focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will ...
Read more >
Focus and text fields - Flutter documentation
When a text field is selected and accepting input, it is said to have “focus.” Generally, users shift focus to a text field...
Read more >
Input doesn't fire focus event when it focused ... - GitHub
It's a problem of react, not DOM method focus() , because with addEventListener('focus', fn) it fire such an event. ... I called this.refs.input....
Read more >
HTML DOM Element focus() Method - W3Schools
Give focus to a text field when the document has been loaded: ... Definition and Usage. The focus() method gives focus to an...
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