Reactjs 如何在nextjs中获取上一页url
我创建了一个_app.js文件,并将此代码添加到该文件中,该文件应将上一个url的状态存储在一个数组中。当尝试访问以前的url时,我只获取当前页面的url _app.js代码的逻辑或将Reactjs 如何在nextjs中获取上一页url,reactjs,next.js,Reactjs,Next.js,我创建了一个_app.js文件,并将此代码添加到该文件中,该文件应将上一个url的状态存储在一个数组中。当尝试访问以前的url时,我只获取当前页面的url _app.js代码的逻辑或将history值传递给其他组件/页面是否有问题 \u app.js import React from 'react'; import App from 'next/app'; class MyApp extends App { static async getInitialProps({ Component
history
值传递给其他组件/页面是否有问题
\u app.js
import React from 'react';
import App from 'next/app';
class MyApp extends App {
static async getInitialProps({ Component, ctx }) {
let pageProps = {};
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx);
}
return { pageProps };
}
state = {
history: []
};
componentDidMount() {
const { asPath } = this.props.router;
this.setState(prevState => ({ history: [...prevState.history, asPath] }));
}
componentDidUpdate() {
const { history } = this.state;
const { asPath } = this.props.router;
if (history[history.length - 1] !== asPath) {
this.setState(prevState => ({ history: [...prevState.history, asPath] }));
}
}
render() {
const { Component, pageProps } = this.props
return <Component history={this.state.history} {...pageProps} />
}
}
export default MyApp;
同样的问题,你有没有得到解决方案?有没有更新?
const CatPage = ({ history }) => {
console.log(history) //I get the value of a current page
console.log(history[0]) // same thing it is just the current url in the array
}