text input focus programatically not working
See original GitHub issueCurrent 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:
- Created 2 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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.
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.