Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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
Javascript 如何将数据添加到数据源而不是替换?_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 如何将数据添加到数据源而不是替换?

Javascript 如何将数据添加到数据源而不是替换?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在开发一个react-native应用程序,在从fetch请求接收数据时,我需要在列表视图中显示它,我在每次提取时都会收到我想要添加到以前接收到的数据中的数据,但我下面的代码只是替换了它,我对react-native和JavaScript都是新手,所以请解释一下如何实现它 fetch("http://example.com/getDataFromIndex.php", { method: 'POST', headers: new Headers({

我正在开发一个react-native应用程序,在从fetch请求接收数据时,我需要在列表视图中显示它,我在每次提取时都会收到我想要添加到以前接收到的数据中的数据,但我下面的代码只是替换了它,我对react-native和JavaScript都是新手,所以请解释一下如何实现它

fetch("http://example.com/getDataFromIndex.php", {
        method: 'POST',
        headers: new Headers({
            'Content-Type': 'application/x-www-form-urlencoded', // <-- Specifying the Content-Type
        }),
        body: params // <-- Post parameters
    }).then((response) => response.json())
        .then((responseJson) => {

            this.setState({
                isLoading: false,
                dataSource: responseJson.data,
            }, function(){

            });

        })
        .catch((error) =>{
            console.error(error);
        });
this.setState({
                isLoading: false,
                dataSource: {...this.state.dataSoure, ...responseJson.data}
            }, function(){
            // do something after `setState` has occurred (asynchronously)
        })
fetch(“http://example.com/getDataFromIndex.php", {
方法:“POST”,
标题:新标题({
“内容类型”:“应用程序/x-www-form-urlencoded”,//{
这是我的国家({
孤岛加载:false,
数据源:responseJson.data,
},函数(){
});
})
.catch((错误)=>{
控制台错误(error);
});

我想将数据附加到数据源,而不是仅仅替换旧的数据源。

假设
dataSource
responseJson.data
都是数组,您可以
concat
它们

this.setState({
  isLoading: false,
  dataSource: Object.assign(this.state.dataSource).concat(responseJson.data)
});
this.setState({
                isLoading: false,
                dataSource: {...this.state.dataSoure, ...responseJson.data}
            }, function(){
            // do something after `setState` has occurred (asynchronously)
        })

假设
dataSource
responseJson.data
都是数组,您可以
concat
它们

this.setState({
  isLoading: false,
  dataSource: Object.assign(this.state.dataSource).concat(responseJson.data)
});
this.setState({
                isLoading: false,
                dataSource: {...this.state.dataSoure, ...responseJson.data}
            }, function(){
            // do something after `setState` has occurred (asynchronously)
        })

您应该使用更新的ECMAScript 6,如下所示:

this.setState({
                isLoading: false,
                dataSource: {...this.state.dataSoure, ...responseJson.data}
            }, function(){
            // do something after `setState` has occurred (asynchronously)
        })

您应该使用更新的ECMAScript 6,如下所示:

this.setState({
                isLoading: false,
                dataSource: {...this.state.dataSoure, ...responseJson.data}
            }, function(){
            // do something after `setState` has occurred (asynchronously)
        })

虽然不需要Object.assign,但只需在dataSource上调用concat并将responseJson.data传递给它就可以完成这个任务,而KDIDN不需要Object.assign,只需在dataSource上调用concat并将responseJson.data传递给它就可以了