Jack23
Jack23

Reputation: 1396

Pass value with a button

I have in my DB values in array.

"ExercisesData": [
    {
      "Id": "01",
      "Reps": "10",
      "StartDate": "2019-06-20",
      "EndDate": "2019-06-21",
      "Notes": "...."
    },
    {
      "Id": "02",
      "Reps": "1",
      "Notes": "....."
    },
    {
      "Id": "03",
      "Reps": "150",
      "Notes": "......"
    }
  ]

I take these values and create a button for them. Like this:

checkAttività (){
   //..... 
}

  render() {
    const exercises = this.state.Exercises.map(exercise => {
      return (
        <View>
          <Text style={{ fontSize: 15 }}>Id: {exercise.Id}</Text>
          <Text style={{ fontSize: 15 }}>Reps: {exercise.Reps}</Text>
          <Text style={{ fontSize: 15 }}>Note: {exercise.Notes}</Text>
          <TouchableOpacity
              style={[styleButton.button, styleButton.buttonOK]}
              onPress={() => this.checkAttività(exercise.Id)}>
            <Text style={styleButton.buttonTesto}>Choose</Text>
          </TouchableOpacity>
        </View>

In this way I create a button for every values in array. What I should do, is to create a button which allows me to pass the id value corresponding to the clicked key. In the sense:

Id: 01

Reps:

Notes: ...

Button1


Id: 02

Reps:

Notes: ...

Button2

If I click on the button1 I would pass the value of the Id (01). How can I do? Thank you.

Upvotes: 1

Views: 88

Answers (2)

meta4
meta4

Reputation: 736

Just add id parameter in checkAttività like:

checkAttività (id){
 //..... 
}

You already have used anonymous function, so it shoud do the trick:

onPress={() => this.checkAttività(exercise.Id)}

Also you should bind your event, eg in constructor:

this.checkAttività = this.checkAttività.bind(this)

Upvotes: 1

Dinesh Kumar
Dinesh Kumar

Reputation: 488

You can do this. Just bind the event and you will be able to pass any params.

onPress={ this.someEvent.bind(this,exercise.Id)}

someEvent(id){
   console.log("You will get the id: ",id);
}

Upvotes: 1

Related Questions