在自學React-Native的過程中。構建一個零售iOS網站的模型,該網站從Firebase獲取數據,並試圖將Navigator納入其中。我的應用程序從'index.ios.js> MainLayout>路由> HomeIndex'流動。這裏據說組件:React-Native - Navigator not rendering initialRoute
MainLayout.js
import React, { Component } from 'react';
import { Text } from 'react-native';
import { Container, Header, Title, Content, Footer, FooterTab, Button, Left, Right, Body, Icon } from 'native-base';
import Routing from './Routing';
import HomeIndex from '../pages/HomeIndex';
class MainLayout extends Component {
render() {
return (
<Container>
<Header>
<Left>
<Button transparent>
</Button>
</Left>
<Body>
<Title>RetailSite</Title>
</Body>
<Right />
</Header>
<Content>
<Routing />
</Content>
<Footer>
<FooterTab>
<Button full>
<Text>Footer</Text>
</Button>
</FooterTab>
</Footer>
</Container>
);
}
}
export default MainLayout;
Routing.js
import React, { Component } from 'react';
import { Navigator, View } from 'react-native';
import HomeIndex from '../pages/HomeIndex';
import ShowPage from '../pages/ShowPage';
class Routing extends Component {
renderScene(route, navigator) {
if (route.name === 'home') {
return <HomeIndex navigator={navigator} />
} else if (route.name === 'show') {
return <ShowPage navigator={navigator} />
}
}
render() {
return (
<Navigator
initialRoute={{ name: 'home' }}
renderScene={this.renderScene.bind(this)}
/>
);
}
}
export default Routing;
HomeIndex.js
import React, { Component } from 'react';
import { RNRSData } from '../../config/FirebaseConstants';
import { StyleSheet, Text, View, TouchableHighlight, Image, Navigator } from 'react-native';
import styles from '../../../styles.js';
class HomeIndex extends Component {
constructor(props){
super(props);
this.state = {
allProducts: [],
}
}
componentDidMount() {
let _this = this;
let products = {};
RNRSData.ref('products/').once('value', function(data) {
data.forEach(function(productNode) {
let name = productNode.val().name;
let cost = productNode.val().cost;
let image = productNode.val().image;
let idNumber = productNode.key;
products[name] = {name: name, cost: cost, image: image, id: idNumber}
})
}).then(function(product) {
console.log(product)
let keys = Object.keys(products).sort();
let sortedHash = {};
let sortedArray = [];
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
sortedArray.push(sortedHash[key] = products[key]);
}
_this.setState({allProducts: sortedArray}, function afterProductSet() {
});
})
}
render() {
let allProducts = this.state.allProducts;
console.log(allProducts);
return (
<View>
<Text style={styles.homeHeader}>New Arrivals</Text>
<View style={styles.homeIndexContainer}>
{allProducts.map(function(object) {
return (
<View style={styles.liContainer} key={object.id}>
<Image source={{uri: object.image}} style={styles.liImage}>
<Text style={styles.liTextName}>{object.name}</Text>
<Text style={styles.liTextCost}>${object.cost}</Text>
</Image>
</View>
)
})}
</View>
</View>
)
}
}
export default HomeIndex;
既然這樣,我的模擬器呈現頁眉和頁腳並在HomeIndex中點擊console.log並在所述console.log中顯示正確的數據。但它並沒有顯示它在我的模擬器中從Firebase提取的數據。
如果我在MainLayout.js中將<Routing />
換爲<HomeIndex />
,它會正確地在模擬器中顯示索引,這導致我懷疑它可能是我在Routing.js中處理的東西。或者,如果導航器以某種方式拋出我的componentDidMount方法。任何幫助,將不勝感激。
所以它的工作,但它沒有顯示。我認爲你可能需要將你的導航器圍繞一個視圖用flex:1. –
@MattAft是啊我已經嘗試在View中包裝導航器,並且在Views中我的renderScene條件下包含HomeIndex和ShowPage。這兩種方法似乎都沒有什麼好處。 – mcw