Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native modal issue when containing TextInput and Button

Tags:

react-native

Steps to replicate:

1) Use React Naitve modal 2) Modal contains TextInput and a button 3) enter some text in TextInput and click on button 4) on the first click nothing happens. Keywoard just disappears 5) on the second click text is sent back to whoever called this modal.

class ReplyModal extends Component <Props, State> {

  state = { modalVisible: false, reply: '' };

  setModalVisible(visible) {
    this.setState({ modalVisible: visible });
  } 
  componentDidMount() {
    this.setState({ modalVisible: this.props.modalVisible });
  }
  componentWillReceiveProps(nextProps) {
    this.setState({ modalVisible: nextProps.modalVisible });
  }
  onSubmitReply = () => {
    this.setState({ modalVisible: false });
    this.props.onSubmitReply(this.state.reply);
  } 
  render() {
    return (

      <Modal
        animationType={'slide'}
        transparent={true}
        visible={this.state.modalVisible}
        onRequestClose={() => {
          alert("your data is saved.");
        }}
      >
        <View style={styles.modalViewOuter}>
          <View style={styles.modalViewInner}>
            <View style={{ flexDirection: 'row', justifyContent:'flex-end' }}>
              <TouchableOpacity onPress={() => this.setState({ modalVisible: false })} >
                <MaterialIcons name="close" color="grey" size={25} />
              </TouchableOpacity>
            </View>


            <FormInput value={this.state.reply} 
              placeholder="Reply to the comment"
              onChangeText={(reply) => this.setState({ reply })}
            />

            <Button
              backgroundColor="#03A9F4"
              buttonStyle={{ borderRadius: 0, marginLeft: 0, marginRight: 0, marginBottom: 0 }}
              title='Submit Reply'
              onPress={this.onSubmitReply}
            />

          </View>
        </View>
      </Modal>
    );
  }
}

Issue remains true with 1) TextInput or FormInput 2) Button or TouchableOpacity or anything similar.

Edit: The same issue occurs if on android i click back (on the bottom of screen; next to home button). The first time keyboard disappears and the second time clicking on back button -> Modal disappears.

like image 301
sushil bansal Avatar asked Aug 22 '17 20:08

sushil bansal


People also ask

How do I stop react native modal from moving up when keyboard opens Android?

Try giving {position: 'absolute'} in its style props!

How do you move a view up when keyboard appears react native?

How do you control what keyboard pushes up React Native? Use android:windowSoftInputMode="adjustResize". KeyboardAvoidingView and other keyboard-related components don't work quite well if you have "adjustPan" set for your android:windowSoftInputMode in AndroidManifest.

What is ref in TextInput react native?

ref attribute on TextInput is used to store a reference to a DOM node. React will call the ref callback with the DOM element when the component mounts, and call it with null when it unmounts.

How dismiss keyboard IOS react native?

Method 1: Using TouchableWithoutFeedback Component: We simply encapsulate the outermost View component of our App inside the TouchableWithoutFeedback component and set the onPress value of this component to Keyboard. dismiss.


1 Answers

I encountered this issue but it was actually caused by the <SectionList>. Adding

<SectionList keyboardShouldPersistTaps="always"/>

fixed my problem

Same thing goes to

<ScrollView>, <FlatList>

like image 157
killebytes Avatar answered Nov 10 '22 22:11

killebytes