0

A .map render some TouchableOpacity with the function showHide on the onPress prop. Everything render well but when I press one of the TouchableOpacity nothing happen.

I've tried many different things:
onPress={this.showHide}
onPress={() => this.showHide}
this.showHide = this.showHide.bind(this)
<ScrollView keyboardShouldPersistTaps="always">

Here's my code :

constructor(props) {
    super(props)
    this.state = { show: false }
    console.log(this.state.show)
    // this.showHide = this.showHide.bind(this)
}

showHide() {
    console.log(this.state.show)
    console.log("**************************************************************")
    alert("SUCCESS !")
    /*this.setState({
        show: !this.state.show
    })*/
    console.log(this.state.show)
}

render() {
    const cards = datas.cards
    return (
        <ScrollView style={styles.scroll}>
            {
                cards.map(function(card, i) {
                    const keys = Object.keys(card)
                    const values = Object.values(card)
                    return(
                        <View key={i}>
                            <TouchableOpacity style={styles.titleView} key={i} onPress={this.showHide}>
                                <Text style={styles.titleText}>{card.title}</Text>
                            </TouchableOpacity>
                                {
                                    keys.map(function(key, j) {
                                        if(key != "title") {
                                            return(
                                                <View style={styles.row} key={j}>
                                                    <Text style={styles.titleCell} textStyle={styles.text}>{key}</Text>
                                                    <Text style={styles.valueCell}>{values[j]}</Text>
                                                </View>
                                            )
                                        }
                                    })
                                }
                        </View>
                    );
                })
            }
        </ScrollView>
    )
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • Try changing your declaration of `showHide()` to `showHide = () => ` also change your `.map(function(card, i)` to `.map((card, i) =>` – Andrew Mar 01 '19 at 13:14
  • What happens if you do `onPress={() => alert('Test!')}` ? – Lucas Bustamante Mar 01 '19 at 13:19
  • 2
    I changed `showHide()` to `showHide = () =>`, `.map(function(card, i)` to `.map((card, i) =>` and `onPress={this.showHide()}` to `onPress={() => this.showHide()}` and now that's working. I think this kind of mistake is pretty common but I never thought about it. Thanks for your help – Ariel Munera Mar 01 '19 at 13:38
  • is it resolved? – Jitender Dev Apr 12 '21 at 08:05
  • With what I've said in my message on Mar 1 '19 at 13:38 it has been resolved, yes. – Ariel Munera May 19 '21 at 09:55

0 Answers0