react-native-modal: The keyboard does not pop up when the Android Textinput autoFocus is true
Environment
Platforms
only happen android
Versions
- Android:
- iOS:
- react-native-modal:11.5.3
- react-native:0.63.3
- react:
Description
Reproducible Demo
Here is our modal code <Modal isVisible={edit} style={styles.modalStyle} backdropTransitionOutTiming={0} onBackdropPress={() => this.resetState()} animationIn=“fadeInUp” animationOut=“fadeOutDown” avoidKeyboard={true} > <View style={{ backgroundColor: ‘white’, width: deviceWidth, paddingVertical: 10, borderTopLeftRadius: 10, borderTopRightRadius: 10, }} > <TextInput value={content} multiline={true} autoFocus={true} placeholder={‘回复本帖:’} maxLength={140} ref={(ref) => (this.contentInput = ref)} onBlur={this.contentInputBlur} onFocus={this.contentInputFocus} underlineColorAndroid=“transparent” onChangeText={(v) => this.setState({ content: v })} style={{ height: 100, width: deviceWidth - 30, backgroundColor: ‘#F7F7FB’, textAlignVertical: ‘top’, borderRadius: 8, alignSelf: ‘center’, padding: 15, }} /> </View> </Modal>
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 15
- Comments: 16
It will work:
It’s a solution <Modal isVisible={true} onModalShow={() => { this.inputRef.blur(); this.inputRef.focus(); } > <TextInput ref={ref => inputRef = ref} /> </Modal>
This took me ages to figure out
also works in an
useEffect:And it only works when calling
.blur()first! It also works sometimes without thesetTimeoutbut is more reliable with it.my solution delay to load modal const inputTitle = useRef<TextInput>() <Modal isVisible={true} onShow={() => { const timeout = setTimeout(() => { inputTitle.current.focus() }, 100); return () => clearTimeout(timeout) } > <TextInput ref={inputTitle}/>
I encounterred this issue too…
When I find time this weekend I will creat a snack with a resolution.
Further applies only for Android, did not test that for IOS! I consider this as a workaround (like others). On Emulator autofocus actually works as intended! But on physical device it focused textInput but keyboard did NOT show up.
As i am beginner i am not sure if working without useRef is desirable or not, but when i tried to use solutions above i encountered warnings that i should use forwardRef, because of functional component.
My solution using state + autofocus instead of Ref + timeouts:
const [showTextInput, setShowTextInput] = useState(false);This is basically the same as @maXXCZ1 solution, but with an added twist. It doesn’t require setTimeout, seems to work every time, visually the TextInput and the modal appear to show at the same time, and the keyboard doesn’t show twice while opening, which I noticed happening with some of the other solutions.
Short Answer:
Long Answer
Hi it looks like 11.10.0 is still affected by this.
Any hint to start working on a PR? I’m willing to help 😃