2

I use the same construction onPress={this.onSave} But I don't know how to pass value to function onSave.

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
Ignat
  • 3,727
  • 6
  • 16
  • 17
  • 1
    You can do it the same way you do it without react or react-native. However, if that component is rerendered a lot, you should create a new component which gets passed the callback and the value to pass to it, so that you don't have to create a new function in each render. I suggest to have a look at http://stackoverflow.com/questions/32366248/react-ineffiencies-of-binding-a-new-function-for-each-event – Felix Kling Feb 02 '16 at 14:59

2 Answers2

4
onPress={ () => this.onSave(value) }
Nader Dabit
  • 52,483
  • 13
  • 107
  • 91
  • I like this solution more, however it's not widely supported in browsers as of now. More info [on mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – kecer Feb 02 '16 at 15:04
  • You are right, but when using React Native, the code is compiled with babel and ES6 works out of the box. – Nader Dabit Feb 02 '16 at 15:33
2

You can use .bind

onPress={ this.onSave.bind(this, 10) }
Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144