user17719642
user17719642

Reputation: 49

Passing a value to onPress to trigger a function


  const change = (e) => {
    console.log(e)
    //Firebase.database().ref(`userSubject/${subjectTaken.user}/${subjectTaken.id}/topics/${name}/`).update({"completed": !completed})
  }


  return (
    <View style={styles.page}  >
      <Header type="dark-profile" title={subjectTaken.name} desc="Here is your progress. You can do this!" onPress={() => navigation.goBack()}/>
      <View>
        {topic.map(item => {
          if(item.completed === true) {
            return <TodoBox text={item.lesson} type={true} onPress={() => console.log("YEY")} />
          } else {
            const name = item.lesson
            return (
              
              <TodoBox text={item.lesson} type={false} onPress={(name) => {this.change(item.lesson)}} />
            )
          }
        })}
      </View>
    </View>
  )
}


I am wondering how i can pass the variable item.lesson to the function that i inserted on onpress... log gives me undefined why

Upvotes: 0

Views: 105

Answers (1)

Shakif Malek
Shakif Malek

Reputation: 1

In this snippet, you are passing name as a parameter, so the function does not know about item.

Also, it looks like the onChange function requires an event as a parameter. So, changing the following line:

<TodoBox text={item.lesson} type={false} onPress={(name) => {this.change(item.lesson)}} />

to this:

<TodoBox text={item.lesson} type={false} onPress={(e) => {this.change(e)}} />

might fix this.

Finally, please try to the complete code if possible.

Upvotes: 0

Related Questions