Javascript React native:将字符串作为函数传递
操作允许我从按钮加载页面。因此,我将“page1”作为一个键(这是一个函数),我可以应用于Actions.page1,它将导航到该页面。但是,这里我只得到this.props.lec的字符串,它包含与我想要的键/函数相同的字符。因此,在构建时会收到错误消息:“category不是一个函数(计算“\u reactNativeFlux.Actions.a({id:this.props.id,title:this.props.title})”。因此我相信它不会工作,因为它是一个字符串而不是函数。那么我如何将它作为函数呢Javascript React native:将字符串作为函数传递,javascript,reactjs,react-native,redux,react-native-router-flux,Javascript,Reactjs,React Native,Redux,React Native Router Flux,操作允许我从按钮加载页面。因此,我将“page1”作为一个键(这是一个函数),我可以应用于Actions.page1,它将导航到该页面。但是,这里我只得到this.props.lec的字符串,它包含与我想要的键/函数相同的字符。因此,在构建时会收到错误消息:“category不是一个函数(计算“\u 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});
}
使用方括号,就像使用数组一样
var obj = {
something: 'ok'
}
var key = 'something';
console.log( obj[ key ] ) ==> 'ok'
使用方括号,就像使用数组一样
var obj = {
something: 'ok'
}
var key = 'something';
console.log( obj[ key ] ) ==> 'ok'
就像Ben说的,你需要使用方括号,但是你也需要传递参数 正确的方法是:
const { id, lec, title } = this.props; /* added for simplicity */
Actions[lec].call(undefined, { id, title });
就像Ben说的,你需要使用方括号,但是你也需要传递参数 正确的方法是:
const { id, lec, title } = this.props; /* added for simplicity */
Actions[lec].call(undefined, { id, title });
可能重复的可能重复只是说找不到变量lecah通过-Actions[this.props.lec]使其工作。调用({id:this.props.id,title:this.props.title});只是说找不到变量lecah通过-Actions[this.props.lec]使其工作。调用({id:this.props.id,title:this.props.title});