khai khai
khai khai

Reputation: 35

React Native Alert Function

I want to show the alert function when I press the continue button. But I have an error. I want to put an alert function inside my onPress touchableOpacity. This is my code.

export default class Rate extends Component {
 render() {
    }
return (
          <TouchableOpacity
      activeOpacity={0.7}
      style={styles.button}
      onPress={() => 
        firebase.database().ref(list.title).set({Ratings : (this.state.Default_Rating)})
        alert('Thank you')
    }> 
      <Text>Continue</Text>
    </TouchableOpacity>

  </View>
);  }}

Upvotes: 0

Views: 1948

Answers (2)

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

There is three mistake in your code.

1) You write your logic outside render

2) There is no opening View for  **</View>** in your code.

3) **onPress** is wrongly implemented.

Below is the correct code:

export default class Rate extends Component {
 render() {
          return (
            <TouchableOpacity
                activeOpacity={0.7}
                style={styles.button}
                onPress={() => {
       // Here you need to keep both brackets for your method
                    firebase.database().ref(list.title).set({ Ratings: (this.state.Default_Rating) })
                    alert('Thank you')
                }}>
                <Text>Continue</Text>
            </TouchableOpacity>
        );

    }    
  }

Upvotes: 1

Patel Dhara
Patel Dhara

Reputation: 886

Your render method should be like as below and add one more bracket in onPress of TouchableOpacity

 render() {
    return (
          <TouchableOpacity
          activeOpacity={0.7}
          style={styles.button}
          onPress={() => {
            firebase.database().ref(list.title).set({Ratings : 
      (this.state.Default_Rating)})
            alert('Thank you')
        }
        }> 
          <Text>Continue</Text>
        </TouchableOpacity>
        );
       }

Upvotes: 0

Related Questions