简体   繁体   中英

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!

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

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

As you can see the Adder takes only one argument but you are passing three the props is a single object

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

You should have only one object: new Adder({callback:this.resultFunc, valueA:4, valueB:2}) this will be your props.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM