react-native: IOS TouchableHighlight need tap twice to trigger onPress function

In my Project, I have a TextInput and a TouchableHighlight , the code is below:

export default class CommentInput extends Component {
    static propTypes = {
        visible: React.PropTypes.bool,
        onFocus: React.PropTypes.func,
        placeholder: React.PropTypes.string,
        text: React.PropTypes.string,
        onSubmit: React.PropTypes.func,
        onHideTextInput: React.PropTypes.func
    };
    constructor(props) {
        super(props);
        this._textInput = null;
        this.state = {
            visible: this.props.visible || false,
            placeholder: this.props.placeholder || '',
            text: this.props.value || '',
            keyboardHeight: 0
        }
    }
    componentWillUnmout() {
        console.log(this._textInput);
        this._textInput && this._textInput.blur();
    }
    renderInput = () => {
        return <View style={styles.container}>
                <TextInput
                    textAlignVertical="top"
                    underlineColorAndroid="transparent"
                    value={this.state.text}
                    onFocus={this.props.onFocus}
                    autoCapitalize="none"
                    allowFontScaling={false}
                    onChangeText={this._onChangeText}
                    ref={textInput => {this._textInput = textInput}}
                    placeholder={this.state.placeholder}
                    multiline={true}
                    autoFocus={true}
                    onBlur={this._textPress}
                    onPress={this._textPress}
                    style={styles.textInput} />
                <View style={styles.btnWrap}>
                    <Button
                        underlayColor={'#f00'}
                        onPress={this.state.text ? this._checkLogin : null}
                        textStyle={styles.btnText}
                        style={[styles.btn, this.state.text.trim() ? null : styles.opacity]}>发表</Button>
                </View>
                {
                    Platform.OS === 'ios' ?  <KeyboardSpacer/> : null
                }
            </View>
    };
    _onChangeText = (text) => {
        this.setState({
            text: text
        });
        this.props.onChangeText && this.props.onChangeText(text);
    };
    _checkLogin = async () => {
        let isLogin = await invoke('Account.isLogin');
        if (isLogin.result) {
            this._onSubmit();
        }
        else {
            let data = await invoke('Account.login', {source: 'Comment'});
            if (data.result) {
                this._onSubmit();
            }
        }
    };
    _onSubmit = () => {
        this.props.onSubmit(this.state.text);
    };
    _textPress = () => {
        alert('123');
    };
    render() {
        return this.props.visible ? this.renderInput() : null
    }
}

and in another file:

{
                  this.state.showMask ? <TouchableWithoutFeedback ll="ll" onPress={this._hideTextInput}>
                      <View style={styles.markView}></View>
                  </TouchableWithoutFeedback> : null
              }
              <CommentInput
                  onSubmit={this._listenSubmit}
                  visible={this.state.showMask} />

when I use the keyboard to input some words and press the TouchableWithoutFeedback at the same times, several times , the TouchableWithoutFeedback need to tap twice to trigger the onPress function. how to solve this bug?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 6
  • Comments: 35 (7 by maintainers)

Most upvoted comments

Are you using a ScrollView? By default when an input is focused the tap will close the keyboard but not trigger any other touch events. You can change that behavior using http://facebook.github.io/react-native/docs/scrollview.html#keyboardshouldpersisttaps

I use a FlatList instead a ScrollView. In my case, keyboardShouldPersistTaps fixes issues.

The original issue of needing two taps to activate onPress is still exits. I don’t know why it’s been closed. The original code still reproduces the issue in latest releases.

keyboardShouldPersistTaps doesn’t work on iOS(react-native 0.51.0).

Solved it by making sure that all parent ScrollView Props keyboardShouldPersistTaps is set to “handled”.

@vinayan3, Since there are a million threads already about keyboardShouldPersistTaps on ScrollView, and this one is specifically not about ScrollView, it would be best to keep this out of the discussion unless the suggestion is to add keyboardShouldPersistTaps to View or other Components.

I have this problem on 0.41.0 with a ScrollView which contains TouchableHighlights.

In my case keyboardShouldPersistTaps fixes issues.

Facing the same issue here. A simple Input and a TouchableOpacity. Adding a ScrollView around with the keyboardShouldPersistTaps='always' does not help. Anyone with a solution?

@hnanh is right, keyboardShouldPersistTaps doesn’t work on RN 0.51+