Next.js: Router.push with state

后端 未结 2 2028
暖寄归人
暖寄归人 2021-01-01 20:22

I\'m using next.js for rebuilding an app for server side rendering. I have a button that handles a search request.

In the old app, the handler was this one:

2条回答
  •  猫巷女王i
    2021-01-01 21:03

    In next.js you can pass query parameters like this

    Router.push({
        pathname: '/about',
        query: { name: 'Someone' }
    })
    

    and then in your next page (here in /about page), retrieve the query via the router props, which needs to be injected to Component by using withRouter.

    import { withRouter } from 'next/router'
    
    class About extends React.Component {
      // your Component implementation
      // retrieve them like this
      // this.props.router.query.name
    }
    
    export default withRouter(About)
    

提交回复
热议问题