FabricioG
FabricioG

Reputation: 3320

Passing an event and additional variable in React Native

I'm currently working on a react native project using expo.

I have an input

<Input type="text" onChange={this.onValueChange} />

When a change is made in the type I can console.log like so:

  onValueChange(event) {
    console.log(event.nativeEvent.text);
  }

However I need to pass an additional variable to this method so I can identify it. I tried this:

  onValueChange(event, inputArea) {
    console.log(event.nativeEvent.text);
  }

And to pass it the variables I did this:

<Input type="text" onChange={this.onValueChange(event, 'nine')} />

Event now does nto function. I also tried:

<Input type="text" onChange={this.onValueChange('nine')} />

My outcome is that I need to capture the input text AND pass a variable.

Upvotes: 0

Views: 101

Answers (2)

Hurobaki
Hurobaki

Reputation: 4078

You can use an high order function.

onChangeHandler = (foo, bar) => event => {
  console.log(foo) // should display nine
  console.log(bar) // should display whatever

  // handle your event with your native variable 
}

<Input type=« text » onChange={this.onChangeHandler(‘nine’, ‘whatever’)} />

Upvotes: 1

FabricioG
FabricioG

Reputation: 3320

Just saw how to do this.

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

Upvotes: 1

Related Questions