Search code examples
javascriptreactjsreact-nativereduxreact-native-router-flux

React native: pass string as function


Actions allows me to load a page from a button. So I would have e.g. 'page1' as a key (which is a function), that i could apply to Actions.page1 and it would navigate to that page. However, here I am only getting the string of this.props.lec, that holds the same characters as the key/funcion that I want. Thus when building get the error message of: 'category is not a function (evaluating '_reactNativeFlux.Actions.a({id: this.props.id, title: this.props.title})'. So I believe it will not work because its a string and not a function. So how would i make it as a function?

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

Solution

  • Like Ben said, you will need to use square brackets. However you need to pass arguments as well.

    The correct way to go is:

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