在转换之间保存和恢复状态的最佳方法

时间:2016-07-18 20:25:16

标签: reactjs react-router react-redux react-router-redux

我正在尝试使用向后和向前功能实现分页,但问题是如何在转换和导航恢复之间保持状态?

我试图通过以下方式实现react-router的实现:

搜索结果组件

handleNextPage(){
        this.props.push({
            pathname: '/search_result',
            query:{
                key:key,
                location:location,
                page: eventKey
            },
            state:{
                content:this.props.searchResult.content,
                number: eventKey
            }
        });
    }

然后在reducer:

import {LOCATION_CHANGE} from 'react-router-redux';
export default function (state = INIT_STATE, action) {
switch (action.type) {
    case LOCATION_CHANGE:
        let {pathname} = action.payload;
        if (pathname === PATHNAME && (action.payload.state !== null)) { 
                return {...state, content: action.payload.state.content, number: action.payload.state.number}
        }
}

return state;

}

问题在于,当启动SearchResult组件时,来自服务器的数据尚不可用,因此空数组将存储为当前位置的状态。

提前感谢您提出解决问题的方法。

1 个答案:

答案 0 :(得分:1)

我建议您仔细阅读redux git repository中的示例。当您到达包含分页的最后一个'真实世界'示例时,您将能够自己回答这个问题。

相关问题