Reactjs/Nextjs-如何使用 NextJS 重定向到另一个页面

use*_*135 4 reactjs next.js

我正在使用 nextjs/reactjs 并需要一些有关如何重定向到页面的帮助

文件夹结构

-Pages

  -business

    -location.js

    -selectlocation.js
Run Code Online (Sandbox Code Playgroud)

URL = http://myhost/business/ location ?city=Pensacola&state=FL

当参数丢失时,我想重定向到selectlocation

当参数为空时,如何从位置页面路由到选择位置页面

这是我的location.js代码片段

export default class extends Component {
    static getInitialProps({ query: { city, state } }) {
        return { city: city, state: state };
    }
    render() {
        return (
            <div>
                <Head>
                    <title>{title}</title>
                    <meta name="description" content={description} />
                </Head>

                <LayoutWithHeader
                    banner={false}
                    view="business"
                    link="location banner"
                    locationCity={this.props.city}
                    locationState={this.props.state}>
                    <Location />
                </LayoutWithHeader>
            </div>
        );

    }
}
Run Code Online (Sandbox Code Playgroud)

SelectLocation.JS 的代码

export default () => (
    <div>
        <Head>
            <title>Location</title>
        </Head>
        <LayoutWithHeader banner={true} title="Home / Business / Locations" >
            <SelectLocation />
        </LayoutWithHeader>
    </div>
);
Run Code Online (Sandbox Code Playgroud)

Muh*_*Ali 10

示例路由代码:

import { useRouter } from 'next/router'

function Home() {
  const router = useRouter()


  const handleClick = e => {
    e.preventDefault()
    router.push('/some-path')
  }

  return (
    <button type="button" onClick={handleClick}>
      Go Somewhere
    </button>
  )
}

Run Code Online (Sandbox Code Playgroud)

请参阅文档了解更多详细信息。