Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define hash route in next.js?

I had already made a modal component with <HashRouter> in react-router that became active and inactive with hash url i.e modals is inactive when url is /route and modal1 is active when url is /route#modal1. There is any ways to define hash routes in next.js?

like image 809
Masih Jahangiri Avatar asked Feb 03 '20 09:02

Masih Jahangiri


People also ask

Does NextJs use client-side routing?

The Next. js router allows you to do client-side route transitions between pages, similar to a single-page application. A React component called Link is provided to do this client-side route transition.

How routes work in next JS?

Next. js has a file-based routing system in which each page automatically becomes a route based on its file name. Each page is a default exported React component from the pages directory that can be used to define the most common route patterns.

Can we use React router in next JS?

In React JS, we would install a package called react-router-dom to implement routing inside the application. But Next JS has its own inbuilt router from the next/link , with which we can navigate between the pages. Before using the next/link , we need to set up the different pages/routes inside the pages folder.

How do I create a nested route in next JS?

Create a new page named article. js inside the pages folder. Users can implement nested routes by simply nesting folders inside the pages folder just make sure that the file and folder name are the same. Now if you navigate to the URL localhost:300/article/competitive-programming you will see the desired results.


Video Answer


2 Answers

The part of the url starting with the hash symbol (that identifies an html entity) is never sent to the server, so you cant match the url serverside (if the browser loads /route#modal1 the server will load /route) what you can do is :

Option 1 Handle the modal rendering client side, using next/router in your component with something like this :
(im assuming you are using class components)

  import Router from 'next/router'

  ....

  componentDidMount(){
    let id = Router.asPath.match(/#([a-z0-9]+)/gi )
    if(id){
      // i will show the modal
    }else{
      // something else
    }
  }

Option 2 Pass the id to the url without #.
In your server.js add a route similar to this :

  server.get('/route/:id?', (req, res) => {
    let {id} =  req.params 
    return app.render(req, res, '/mypage', { id})
  })

And then grab the id, ex. in getInitialProps

  static async getInitialProps (context) {
    let ctx  = context.query;
    return ctx
  }

And handle the modal

  componentDidMount(){
    let {id} =  this.props    
    if(id){
      // i will show the modal
    }else{
      // something else
    }
  }
like image 135
Nico Avatar answered Oct 12 '22 19:10

Nico


You can add a hash using the next/router.

Some pages I change the component that renders based on a state string, so to make sure I know which route I'm on (and keep the back button working) I add a state string stage as a hash to the URL when it detects a change.

React.useEffect(() => {
  router.push(`/booking#${stage}`)
}, [stage])

This will only work client side though.

like image 3
martinedwards Avatar answered Oct 12 '22 19:10

martinedwards