gpt4 book ai didi

reactjs - Gatsby.js : Navigating with URL Parameters and the Browser Back/Forward Buttons

转载 作者:行者123 更新时间:2023-12-03 13:26:01 25 4
gpt4 key购买 nike

我有一个文章组件,它显示帖子列表。该列表已分页,因此每页最多可显示 10 个帖子。有一个“下一页”按钮,单击该按钮将更新组件状态和相应的 url 参数,如下所示:

第 1 页:/新闻

第 2 页:/news?page=2

第 3 页:/news?page=3

...等等。方式constructor()render()方法设置完毕后,例如,如果用户直接导航到/news?page=3,它将读取此 URL 参数并显示正确的帖子。

我遇到的问题是浏览器后退和前进按钮似乎不会重新呈现页面。因此,如果用户点击“下一页”按钮几次,然后点击后退按钮,URL 将更新,但页面不会重新呈现。有没有办法强制它这样做?

我猜测有一种方法可以通过添加 window.history 监听器来实现此目的,但我不确定是否有推荐的做法与 gatsby-link 一起使用。 .

这是该组件的精简版本供引用:

import React, { Component } from 'react';
import { navigateTo } from 'gatsby-link';
import getUrlParameter from '../functions/getUrlParameter';

export default class extends Component {
constructor(props) {
super(props);

/* external function, will grab value
* of ?page= url parameter if it exists */
const urlParamPage = getUrlParameter('page');
const currentPage = urlParamPage ? urlParamPage : 1;

this.state = {
currentPage
};
}

nextPage() {
const { props, state } = this;

const urlParam = state.currentPage > 1
? `?page=${state.currentPage}`
: '';

navigateTo(props.pathname + urlParam);
this.setState({currentPage: this.state.currentPage + 1});
}

render() {
const { props, state } = this;

const articles = props.articles
.slice(state.currentPage * 10, state.currentPage * 10 + 10);

return (
<div>
<ul>
{articles.map((article) => <li>{article.title}</li>)}
</ul>

<button onClick={() => this.nextPage()}>Next Page</button>
</div>
);
}
}

最佳答案

您的页面不会重新呈现,因为它实际上是同一页面 - 组件已安装。当您在 constructor() 中获取数据时,您的页面不会更新,因为 React 组件的构造函数在安装之前被调用 ( source )。

您所说的urlParam只是componentWillReceiveProps(nextProps)应该在nextProps.location.search中接收的新 Prop 。

编辑:

您必须提升状态,因为只有根组件才会在浏览器后退和前进按钮上接收 props.location。 Articles 组件的 pathname 属性会发生变化,这就是为什么 componentWillReceiveProps 永远不会在这里触发的原因。

代码:

/src/pages/root.js

import React, { Component } from 'react';
import { navigateTo } from 'gatsby-link';

import Articles from '../components/Articles';

export default class Test extends Component {
constructor(props) {
super(props);

this.state = {
currentPage: 1,
data: {}, // your ext data
};

this.nextPage = this.nextPage.bind(this);
}

nextPage() {
const { currentPage } = this.state;
const { pathname } = this.props.location;
const url = `${pathname}?page=${currentPage + 1}`;

this.setState({ currentPage: currentPage + 1 });
navigateTo(url);
}

componentWillReceiveProps(nextProps) {
const { pathname, search } = nextProps.location;
const getParam = /(\d+)(?!.*\d)/;

const currentPage = search !== '' ? Number(search.match(getParam)[0]) : 1;

/* get your ext. data here */
const data = {};

this.setState({ currentPage, data });
}

render() {
const { currentPage, data } = this.state;
return (
<div>
{/* other contents here */}
<Articles
nextPage={this.nextPage}
currentPage={currentPage}
data={data}
/>
</div>
);
}
}

/src/components/Articles.js

import React from 'react';

const Articles = ({ nextPage, currentPage }) => {
return (
<div>
<div>Page: {currentPage}</div>
<button onClick={() => nextPage()}>Next Page</button>
</div>
);
};

export default Articles;

关于reactjs - Gatsby.js : Navigating with URL Parameters and the Browser Back/Forward Buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48938973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com