如何在React中处理多页JSON响应的调度

时间:2018-05-13 07:40:49

标签: reactjs redux

我在JSON

中使用了以下操作
{
    "count": 14,
    "next": "http://localhost:8081/reminders/?page=2",
    "previous": null,
    "results": [
        {
                ......

        }
}

但是我从中获取的API会在多个页面中返回结果。 结果JSON看起来像这样,

React Action

如何在我的React中合并多页Redux响应?

Thunk一起使用Array.prototype.remove = function(item) { // index will have -1 if item does not exist // else it will have the index of 1st item found in array var index = this.indexOf(item); if (index > -1) { // splice() method is used to add/remove items(s) in array this.splice(index, 1); } return index; } var arr = [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4]; // Printing array // [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4]; console.log(arr) // Removing 67 (getting its index i.e. 2) console.log("Removing 67") var index = arr.remove(67) if (index > 0){ console.log("Item 67 found at ", index) } else { console.log("Item 67 does not exist in array") } // Printing updated array // [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4]; console.log(arr) // ............... Output ................................ // [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ] // Removing 67 // Item 67 found at 2 // [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ] > // Defining an array undefined > var arr = [12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34]; undefined > // Getting length of array undefined > arr.length; 16 > // Adding 1 more item at the end i.e. pushing an item undefined > arr.push(55); 17 > arr [ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34, 55 ] > // Popping item from array (i.e. from end) undefined > arr.pop() 55 > arr [ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > // Remove item from beginning undefined > arr.shift() 12 > arr [ 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > // Add item(s) at beginning undefined > arr.unshift(67); // Add 67 at begining of the array and return number of items in updated/new array 16 > arr [ 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > arr.unshift(11, 22); // Adding 2 more items at the beginning of array 18 > arr [ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > > // Define a method on array (temorarily) to remove an item and return the index of removed item; if it is found else return -1 undefined > Array.prototype.remove = function(item) { ... var index = this.indexOf(item); ... if (index > -1) { ..... this.splice(index, 1); // splice() method is used to add/remove items in array ..... } ... return index; ... } [Function] > > arr [ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > > arr.remove(45); // Remove 45 (You will get the index of removed item) 3 > arr [ 11, 22, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > > arr.remove(22) // Remove 22 1 > arr [ 11, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > arr.remove(67) // Remove 67 1 > arr [ 11, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > > arr.remove(89) // Remove 89 2 > arr [ 11, 67, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ] > > arr.remove(100); // 100 doesn't exist, remove() will return -1 -1 >

1 个答案:

答案 0 :(得分:0)

您可以在减速机中处理此问题。您可以调度每个部分的数据(来自api),在您的reducer中,您可以将所有数据推送(合并)到一个对象中。

在第一次api调用之前,您可以创建一个特殊的调度来清除reducer中此特定对象中的数据,以便将upcomming数据存储为新序列。