React native:将字符串作为函数传递

时间:2018-03-25 13:14:24

标签: javascript reactjs react-native redux react-native-router-flux

操作允许我从按钮加载页面。所以我会有例如'第1页'作为一个键(这是一个函数),我可以应用于Actions.page1,它将导航到该页面。但是,这里我只获取this.props.lec的字符串,它包含与我想要的键/功能相同的字符。因此,在构建时会收到以下错误消息:' category不是函数(评估' _reactNativeFlux.Actions.a({id:this.props.id,title:this.props.title})&# 39;。所以我相信它不会起作用,因为它是一个字符串而不是一个函数。那么我将如何将它作为一个函数?

  var a = this.props.lec;
  _onPress() {
    Actions.a({id: this.props.id, title: this.props.title});
  }

2 个答案:

答案 0 :(得分:1)

使用方括号,就像使用数组一样。

var obj = {
    something: 'ok'
}
var key = 'something';
console.log( obj[ key ] ) ==> 'ok'

答案 1 :(得分:1)

像Ben说的那样,你需要使用方括号。但是你也需要传递参数。

正确的方法是:

const { id, lec, title } = this.props; /* added for simplicity */
Actions[lec].call(undefined, { id, title });