react+antd分页 实现分页 返回后 还在当前页

antd PC框架地址:https://ant.design/index-cn

页面跳转后返回和刷新会回到第一页,在组件里增加了hash值,实现了页面跳转后返回和刷新依然显示当前的页面。

import React, { Component } from 'react'
import { Table } from 'antd';
import 'antd/dist/antd.css';

export default class Index extends Component {
    constructor() {
        super()
        this.state = {
            current: parseInt(window.location.hash.slice(1), 0) || 1,  //这是主要的 
   获取当前页面的hash值,转换为number类型
        }
      }
    render() {
        const { current } = this.state;
        const dataSource = [
            {
                key: '1',
                name: '胡彦斌1',
                age: 32,
                address: '西湖区湖底公园1号'
            },
            {
                key: '2',
                name: '胡彦祖2',
                age: 42,
                address: '西湖区湖底公园1号'
            },
            {
                key: '3',
                name: '胡彦祖3',
                age: 42,
                address: '西湖区湖底公园1号'
            },
            {
                key: '4',
                name: '胡彦祖4',
                age: 42,
                address: '西湖区湖底公园1号'
            },
            {
                key: '5',
                name: '胡彦祖5',
                age: 42,
                address: '西湖区湖底公园1号'
            }, {
                key: '6',
                name: '胡彦祖6',
                age: 42,
                address: '西湖区湖底公园1号'
            }
        ];
        const columns = [{
            title: '姓名',
            dataIndex: 'name',
            key: 'name',
        }, {
            title: '年龄',
            dataIndex: 'age',
            key: 'age',
        }, {
            title: '住址',
            dataIndex: 'address',
            key: 'address',
        }
        ];
        return (
            <div>
                <Table
                    columns={columns}
                    dataSource={dataSource}
                    pagination={{
                        onChange: page => {
                            this.setState(
                                {
                                    current: page
                                },
                                () => { window.location.hash = `#${page}`; }  //设置当前页面的hash值为当前page页数
                            );
                        },
                        pageSize: 3,
                        current: current,
                    }}
                />
            </div>
        )
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值