从redux中删除数据并保存滚动位置

时间:2017-12-29 17:38:05

标签: javascript reactjs scroll redux

我在redux中有一个对象数组,我在浏览器中显示:

  

[{...},{...},{...},{...},{...},{...},{...},{...},{...},{...},{...},{... },{...},{...},   {...},{...},{...},{...},{...},{...},{...},{...},{...},{...},{...},{...},{... },{...}]

object-0
object-1
object-2
object-3
object-4
object-5
object-6
object-7
object-8
object-9
object-10
object-11
object-12
object-13
object-14
object-15
object-16
object-17
object-18
object-19 <------ scroll position

在向下滚动时,我正在点击api并将对象附加到数组。我已经添加了一个检查,如果数组中的对象数量超过20,那么前10个数据应该被删除。

   case "DELETE_LISTING_TOP_PROFILES":

        let tempListingData = state[action.payload.listingId];          
        let tempListingProfiles = [...state[action.payload.listingId].profiles];            
        tempListingProfiles.splice(0,10);
            state
            ={
                ...state,
                [action.payload.listingId]:{
                    ...tempListingData,
                    profiles: tempListingProfiles
                }
            }

在我的redux中,前10个数据被删除,我的视图变为:

object-10
object-11
object-12
object-13
object-14
object-15
object-16
object-17
object-18
object-19
object-20
object-21
object-22
object-23
object-24
object-25
object-26
object-27
object-28
object-29 <------------ scroll position

您可以观察到滚动位置现在更改为object-29。我希望它保持在第19位,如下所示:

object-10
object-11
object-12
object-13
object-14
object-15
object-16
object-17
object-18
object-19 <----------- scroll position
object-20
object-21
object-22
object-23
object-24
object-25
object-26
object-27
object-28
object-29

因此,在从redux删除数据并向其添加一些数据后,如何保存滚动位置?

1 个答案:

答案 0 :(得分:0)

您可以阅读STR_TO_DATE($yourTime, '%c/%e/%Y %r') 生命周期方法(official documentation)。

getSnapshotBeforeUpdate()功能(official documentation)配对后,它可以帮助您在更改道具更改后将更改应用于refs之前管理滚动位置,例如