Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 反应本机:ListView.DataSource未更新_React Native - Fatal编程技术网

React native 反应本机:ListView.DataSource未更新

React native 反应本机:ListView.DataSource未更新,react-native,React Native,在从服务器获取数据后,我正在尝试更新ListView.DataSource,但没有更新。我有两个组件,一个是进口的,另一个是进口的。 从基本组件中,我尝试更新其他组件中的ListView.DataSource。这是我的密码 index.android.js import React, { Component } from "react"; import { ListView, View, Button, AppRegistry } from "react-native"; import

在从服务器获取数据后,我正在尝试更新ListView.DataSource,但没有更新。我有两个组件,一个是进口的,另一个是进口的。 从基本组件中,我尝试更新其他组件中的ListView.DataSource。这是我的密码

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"
}
});

在您的代码中,您只在FormativeRevisionList的构造函数中设置数据源,这意味着FormativeRevisionList仅在您第一次渲染时渲染给定的电影

要在按下“获取主题”按钮后呈现新列表,需要在数据源接收新道具时再次设置数据源,这可以通过在FormativeDeviceList componentWillReceiveProps中设置来实现


您可以阅读更多有关componentWillReceiveProps的信息,这些信息来自于《魅力工作》。事实上,我是一个新的反应,不知道有多少反应功能。
  componentWillReceiveProps(nextProps) {
    if (nextProps.movies !== this.props.movies) {
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(nextProps.movies)
      })
    }
  }