2016-11-10 69 views
0

我正尝试使用react-native构建应用程序。我有多个类,我想根据状态显示一个类。作为组件名称的React-Native变量

let MyComponent = this.props.navigationState.routes[this.props.navigationState.index].component; 

这给了我字符串“场景1”(女巫是我的组件的名称)

之后,我要显示这样

return <MyComponent />; 

我得到的组件错误:

Expected a component class, got [object Object].

如果我显示这样的部件:

return <Scene1 />; 

它实际上显示我的组件。

有谁知道这两个例子之间什么区别?我无法理解为什么包含字符串的变量与字符串不一样。也许我失去了一个非常小的细节,但它只是我不知道什么是错在这里

编辑: 的要求我的路线

class Scene1 extends Component { 

    render() { 

     return(
      <View style={{flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'yellow'}}> 
       <Text>Scene1</Text> 
       <View> 
        <Text onPress={() => this.props.navigate({ type: 'push', key: 'scene2' })}>Go to Scene2</Text> 
       </View> 
      </View> 
     ); 
    } 

} 
+0

你可以在这里添加你的路线 –

+0

更新了我的文章,你的意思是? – vanBrunneren

+0

你可以在'this.props.navigationState.routes [this.props.navigationState.index]'和'this.props.navigationState'上做一个console.log吗?并添加你定义'navigationState'的文件?这个错误意味着你将一个对象而不是一个函数/类作为'MyComponent'传递,这让我觉得无论你在哪里设置navigationState.routes.component,你都使用了一个字符串而不是实际的类。 –

回答

0

我发现同样的事情,我不知道到底JSX解析器应用了什么规则。我可以告诉你,这个工程:

<this.props.componentClass /> 
+0

是的,这是可行的,但你不能用一个复杂的对象与数组在其中,就像我想用我的this.props.navigationState.routes [this.props.navigationState.index ]。零件 – vanBrunneren