Jack23
Jack23

Reputation: 1396

Create an Alert when a button is pressed

I need to create an alert to say if the user is sure to start an activity or not. I have done in this way, but it's incorrect.

What can I do??

const exercises = this.state.Exercises.map(exercise => {
  return (

      <View key={exercise.Id}>
      <Text style={{ fontSize: 15 }}>Id: {exercise.Id} </Text>
      <Text style={{ fontSize: 15 }}>Rep: {exercise.Reps}</Text>
      <Text style={{ fontSize: 15 }}>Note: {exercise.Notes}</Text>
      <TouchableOpacity
        style={[styleButton.button, styleButton.buttonOK]}
        onPress={() => Alert.alert(
          'Do you want to start?',
         [
         { text: 'Si', onPress: () =>  this.check(exercise.Id)},
          {text: 'No', onPress: () => this.homepage()} 
         ] 
        )}              
      >
        <Text style={styleButton.buttonTesto}>Scegli</Text>
      </TouchableOpacity>
      </View>

  );
});

this is the error:

button slice is not a function

Upvotes: 0

Views: 90

Answers (2)

Ramana Sakhavarapu
Ramana Sakhavarapu

Reputation: 124

use this

Alert.alert(
  'Do you want to start',
  [
    {
      text: 'No',
      onPress: () => console.log('Canceled'),
      style: 'cancel',
    },
    {text: 'Yes', onPress: () => console.log('Yes')},
  ],
  {cancelable: false},
);

Upvotes: 1

syjsdev
syjsdev

Reputation: 1336

try like this.

...
onPress={() =>
          Alert.alert('Do you want to start?', '', [
            { text: 'yes', onPress: () => this.check(exercise.Id) },
            { text: 'No', onPress: () => this.Homepage() },
          ])
        }
...

Alert.alert has 3 arguments: Alert.alert(headerText, bodyText, buttons).

you missed one string argument.

Upvotes: 2

Related Questions