Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOS TouchableHighlight need tap twice to trigger onPress function #12784

Closed
avivaWang opened this issue Mar 8, 2017 · 35 comments
Closed

IOS TouchableHighlight need tap twice to trigger onPress function #12784

avivaWang opened this issue Mar 8, 2017 · 35 comments
Assignees
Labels
Platform: iOS iOS applications. Resolution: Locked This issue was locked by the bot.

Comments

@avivaWang
Copy link

avivaWang commented Mar 8, 2017

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?

@janicduplessis
Copy link
Contributor

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

@avivaWang
Copy link
Author

Dear @janicduplessis , the CommentInput component did not included in a ScrollView, at first , the Button(TouchableHightlight) can receive the onPress method, but after several quick open the CommentInput component use keyboard to input some text and at the same time press the background to close the CommentInput., next Time the Button component won't accept the onPress method at the first tap, and the second time the Button works. I don't know why. I also search a lot of issues to find the solution. but I did get the answer

@RichardLindhout
Copy link

I also have this problem sometimes on 0.43

@shergin shergin added the Platform: iOS iOS applications. label Mar 16, 2017
@avivaWang
Copy link
Author

@RichardLindhout did you find the solution?

@shergin
Copy link
Contributor

shergin commented Mar 17, 2017

Probably same problem as #12976 is.

@shergin
Copy link
Contributor

shergin commented Mar 17, 2017

@avivaWang How hard is it to reproduce this problem?

@RichardLindhout
Copy link

I think it has something to do with a busy JavaScript thread.

@shergin
Copy link
Contributor

shergin commented Mar 17, 2017

Yeah, probably. I hope fixing #12976 also will fix this problem.

@vinayan3
Copy link

vinayan3 commented Mar 28, 2017

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

In my case keyboardShouldPersistTaps fixes issues.

@mienaikoe
Copy link

@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.

@agersoncgps
Copy link

agersoncgps commented May 24, 2017

This solved it for me: https://stackoverflow.com/questions/42808294/react-native-have-to-double-click-for-onpress-to-work

@shergin shergin self-assigned this May 25, 2017
@shergin
Copy link
Contributor

shergin commented May 25, 2017

... Speaking about scrollviews, <TextInput multiline={true}/> actually contains <ScrollView> subclass inside.

@mienaikoe
Copy link

mienaikoe commented Jun 1, 2017

@avivaWang While I didn't find a solution, there is a workaround for the case when your keyboard has been dismissed but the focus of the TextInput is still preventing your buttons from being clicked. If you use a keyboard listener from the Keyboard API, you can blur the input when the keyboard is dismissed:

    componentDidMount(){
        this.keyboardDidHideListener = Keyboard.addListener("keyboardDidHide", () => {
          this.refs.input.blur();
        });
      }

    componentWillUnmount(){
        this.keyboardDidHideListener.remove();
    }

@hramos
Copy link
Contributor

hramos commented Aug 16, 2017

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

@hramos hramos closed this as completed Aug 16, 2017
@Cool04ek
Copy link

Cool04ek commented Nov 1, 2017

I think it is related to this issues #15606

@Hugh99
Copy link

Hugh99 commented Jan 4, 2018

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

@shemdani
Copy link

shemdani commented Jan 5, 2018

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.

@koswarabilly
Copy link

I develop android app
both flatlist and scrollview fix by using keyboardShouldPersistTaps

@vuphuctho
Copy link

FlatList does not have property keyboardShouldPersistTaps. My React-native version is 0.51.0.

@koswarabilly
Copy link

@vuphuctho they do, flatlist inherate scrollview props dude..

@hnanh
Copy link

hnanh commented Feb 28, 2018

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

@donnes
Copy link

donnes commented Mar 7, 2018

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

@dsc-bon
Copy link

dsc-bon commented Mar 21, 2018

+1 on this issue. Still need to double tap before the item receives the tap.

@DavitVosk
Copy link

DavitVosk commented Mar 22, 2018

Did anyone solve this problem? I am using RN 0.52.2

@dsc-bon
Copy link

dsc-bon commented Mar 22, 2018

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

@DavitVosk
Copy link

DavitVosk commented Mar 22, 2018

what fixed it for me is to make sure that all nested scrollviews have this property set: keyboardShouldPersistTaps='always'.

This won't work because the parent ScrollView does not have this property set.

@dsc-bon
Copy link

dsc-bon commented Mar 22, 2018

I see, have you tried using a different Touchable component? Like TouchableOpacity

@DavitVosk
Copy link

yes @dsc-bon. I was working with TouchableOpacity and it worked just giving all ScrollViews the same keyboardShouldPersistTaps='always' prop. Thanks

@Niranjan-B
Copy link
Contributor

I was facing the same issue with FlatList (RN -> 0.52.0). But resolved when used
keyboardShouldPersistTaps='always' prop.

@cglacet
Copy link

cglacet commented Apr 27, 2018

Is there any solution not involving the addition of a scrollview (or any other component)?

@msevestre
Copy link

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?

@dsc-bon
Copy link

dsc-bon commented Jun 6, 2018

@msevestre make sure that ALL child container were placed inside the ScrollView

@msevestre
Copy link

What do you mean with child container? ;+)

I have a modal view with one input and two buttons.
I tried to add a scroll view wrapping the input and the buttons but this does not change this behavior.

Cheers

@ocatave
Copy link

ocatave commented Jul 24, 2018

@msevestre
I had the same trouble. In my case, I solved it by adding keyboardShouldPersistTaps="handled" to parent ScrollView of my Modal.
As I figured out parent ScrollView also affects children Modal.
I mean
<ScrollView keyboardShouldPersistTaps="handled"> ... <Modal>...</Modal> ... </ScrollView>

@ericjames
Copy link

This issue goes away when switching to TouchableOpacity

@facebook facebook locked as resolved and limited conversation to collaborators Aug 16, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Aug 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Platform: iOS iOS applications. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests