我正嘗試使用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>
);
}
}
你可以在這裏添加你的路線 –
更新了我的文章,你的意思是? – vanBrunneren
你可以在'this.props.navigationState.routes [this.props.navigationState.index]'和'this.props.navigationState'上做一個console.log嗎?並添加你定義'navigationState'的文件?這個錯誤意味着你將一個對象而不是一個函數/類作爲'MyComponent'傳遞,這讓我覺得無論你在哪裏設置navigationState.routes.component,你都使用了一個字符串而不是實際的類。 –