Next.js: Router.push with state

后端 未结 2 2011
暖寄归人
暖寄归人 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条回答
  • 2021-01-01 20:50

    If you want 'clean' urls, one way to go about it is to add onClick handler to your link and store required information in context/redux store. It easy to implement if you already have one.

    <Link href='...'>
      <a onClick={()=>{dispatch(....)}}>Link<a/>
    <Link>
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题