我試圖JSON URL調用後更新數據源,但ListView控件只是沒有更新,問題是當應用程序加載是第一次,我看不到ListView中的數據,但是,當我使用「熱更新...」時只需按下Ctrl + S在Visual Studio代碼中,列表突然出現在ListView中..陣營本地的,ListView控件+ JSON數據源獲取不更新
可能是什麼問題所以列表不會在第一次加載?只是第二次?
constructor(props) {
super(props);
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows([])
};
}
states = {
index: 1,
routes: [
{ key: '1', title: 'RECOMMANDED' },
{ key: '2', title: 'MY STATIONS' }
]
};
_handleChangeTab = (index) => {
this.setState({ index });
};
_renderHeader = (props) => {
return <TabBar {...props} />;
};
_renderScene = ({ route }) => {
switch (route.key) {
case '1':
return <View style={[ styles.page, { backgroundColor: '#fff' } ]}><ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
enableEmptySections={true}
/></View>;
case '2':
return <View style={[ styles.page, { backgroundColor: '#fff' } ]}><Text>2</Text></View>;
default:
return null;
}
};
componentDidMount() {
fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
var movies = responseJson.movies;
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseJson.movies)
});
}).done();
}
renderRow(dataRow) {
return(
<TouchableHighlight>
<View>
<Text
style={{fontSize: 20, color: '#000000'}}
numberOfLines={1}>
{ dataRow.title } - { dataRow.releaseYear }
</Text>
<View style={{height: 1, backgroundColor: '#dddddd'}} />
</View>
</TouchableHighlight>);
}
render() {
return (
<View style={styles.container}>
<TabViewAnimated
style={styles.tabContainer}
navigationState={this.states}
renderScene={this._renderScene}
renderHeader={this._renderHeader}
onRequestChangeTab={this._handleChangeTab} />
</View>
);
}
我使用react-native-tab-view
模塊。
感謝
'this.states'應該是'this.state' –
我使用'this.states',而不是'this.state'因爲我已經設置好的狀態爲TabNav – John
我現在看到它的工作,但只是後我只是按下「Ctrl + S」和「熱載入」應用程序..否則它顯示爲空,之後它顯示列表...可能是什麼問題? – John