Search code examples
javascriptreact-nativeclassconstructorreact-props

React Native create class with key-value pair props


class Adder extends Component {
  constructor(props) {
    super(props)
    this.props.callback(this.props.valueA + this.props.valueB)
  }
}

In JSX I can do this:

<Adder callback={this.resultFunc} valueA={4} valueB={2}/>

I don't know the syntax in JS, for instance this doesn't work. I only get the first argument passed:

this.myAdder = new Adder({callback:this.resultFunc},{valueA:4},{valueB:2});

Anything but the first KV pair is undefined in the Adder class. Can anyone please point me right? Thanks!


Solution

  • You should be able to put them all in the same object:

    this.myAdder = new Adder({callback: this.resultFunc, valueA: 4, valueB: 2});