Ich versuche, ListView.DataSource
nach dem Abrufen von Daten vom Server zu aktualisieren, aber es ist nicht passiert. Ich habe zwei Komponenten, eine wird in andere importiert. Von der Basiskomponente versuche ich ListView.DataSource
in anderer Komponente zu aktualisieren. Hier ist mein Code.React Native: ListView.DataSource wird nicht aktualisiert
index.android.js
import React, { Component } from 'react';
import {
ListView,
View,
Button,
AppRegistry
} from 'react-native';
import OtherComponent from './Components/OtherComponent'
class MyApp extends Component {
constructor(props) {
super(props);
this.state = {
movies: [{title: 'ABCD'},{title: 'EFGH'}]
};
}
getTopics =() =>{
fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseText) => {
console.log(responseText.movies)
this.setState({
movies: responseText.movies
});
})
.catch((error) => {
console.warn(error);
});
}
render() {
return (
<View>
<Button
onPress={this.getTopics}
title="Get Topics"
color="#841584"
accessibilityLabel="Learn more about this purple button" />
<OtherComponent movies = {this.state.movies} />
</View>
);
}
}
AppRegistry.registerComponent('MyApp',() => MyApp);
OtheComponent.js
import React, { Component } from 'react'
import {
View,
ListView,
Text,
StyleSheet,
Button
} from 'react-native'
export default class FormativeRevisionList extends Component{
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(props.movies)
}
}
render(){
return (
<View>
<ListView
style = {styles.listContainer}
dataSource = {this.state.dataSource}
renderRow = {
(rowData) => (
<View>
<Text style = {styles.listItem}>
{rowData.title}
</Text>
</View>
)
}/>
</View>
)
}
}
const styles = StyleSheet.create ({
listContainer: {
paddingTop: 22
},
listItem: {
fontSize: 30,
fontWeight: 'bold',
textAlign: 'center',
}
})
Arbeitete wie ein Charme. eigentlich bin ich neu zu reagieren und kenne nicht viele reaktionsfunktionen. –