【问题标题】:React native: pass string as functionReact Native:将字符串作为函数传递
【发布时间】:2018-03-25 13:14:23
【问题描述】:

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

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

【问题讨论】:

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


【解决方案1】:

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

正确的做法是:

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

【讨论】:

  • 只是说找不到变量 lec
  • 啊通过 - Actions[this.props.lec].call({ id: this.props.id, title: this.props.title });
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2020-05-01
    • 2015-01-02
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多