Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested routes in Next js

I have a simple SPA and the App and About components are like below. When I click on About link, the about page appears under the links. The problem is that when I click on "About me" link, the "me" page loads instead of about page. What I really want is having nested routing in nextjs. It seems that first-level routes are loading in . But I don't know how to add it to my subcomponents.

import App, { Container } from 'next/app'
import React from 'react'
import Link from 'next/link'
class MyApp extends App {
  static async getInitialProps({ Component, router, ctx }) {
    let pageProps = {}

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx)
    }
    return { pageProps }
  }
  render() {
    const { Component, pageProps } = this.props
    return <Container>
      <ul>
        <li><Link href="/news">News</Link></li>

        <li><Link href="/about">About</Link></li>
      </ul>
      <Component{...this.props} />
    </Container>
  }
}
export default MyApp


import React from 'react'
import Link from 'next/link'
import Router from 'next/router'
class About extends React.Component {
  render() {
    return (
      <div id="main">
        <li><Link href="/about/company">About company</Link></li>
        <li><Link href="/about/me">About me</Link></li>

      </div>

    )
  }
}
export default About
like image 262
Hadi Ranjbar Avatar asked Aug 30 '18 21:08

Hadi Ranjbar


2 Answers

I finally found a solution to this problem. I changed my code according to this issue that suggests using of layout-components: https://github.com/zeit/next.js/issues/4166

class About extends React.Component {
  render() {
    return (
      <div id="main">
        <li><Link href="/about/company">About company</Link></li>
        <li><Link href="/about/me">About me</Link></li>
        {this.props.child}
      </div>

    )
  }
}
export default About

Then what I am going to show as a child should be like this:

const me = () => (
  <About>
    <div>
     About me
    </div>       
  </About>
  )

This prevents page reloading. However, it still rerenders the entire page.

like image 55
Hadi Ranjbar Avatar answered Nov 20 '22 00:11

Hadi Ranjbar


next-routes

there is a very good library for this specific situations that lets you define routes just like well-known express.js routes.

use it.

example from docs:

const routes = require('next-routes')

                                                    // Name   Page      Pattern
module.exports = routes()                           // ----   ----      -----
.add('about')                                       // about  about     /about
.add('blog', '/blog/:slug')                         // blog   blog      /blog/:slug
.add('user', '/user/:id', 'profile')                // user   profile   /user/:id
.add('/:noname/:lang(en|es)/:wow+', 'complex')      // (none) complex   /:noname/:lang(en|es)/:wow+
.add({name: 'beta', pattern: '/v3', page: 'v3'})    // beta   v3        /v3
like image 40
Soorena Avatar answered Nov 20 '22 01:11

Soorena