Coder Social home page Coder Social logo

Comments (3)

fridays avatar fridays commented on May 5, 2024

I didn't try this yet but here is some information that might be helpful: vercel/next.js#88

from next-routes.

AmrN avatar AmrN commented on May 5, 2024

Hey @fridays,
in order to prevent the top-level component from unmounting on route change, I'm thinking about using a single page pages/index.js which is responsible for choosing which component to render.
But I don't know how to use this method with next-routes because I need a way to tell the index.js page which component it should render via the routes definition, maybe something like explicit params.

// routes.js
routes.add('blog', '/blog', 'index', {component: "BlogList"})
routes.add('blog-post', '/blog/:slug', 'index', {component: "BlogPost"})

// pages/index.js
switch (this.props.url.query.component) {
  case 'BlogList':
    // render BlogList
  case 'BlogPost': 
    // render BlogPost
}

So is there anyway I can specify explicit query params like {component: "BlogList"} in the example above, or would you suggest a better way for approaching this problem? Thanks.

from next-routes.

fridays avatar fridays commented on May 5, 2024

Hi @AmrN you can do something like this:

// pages/index.js
import React from 'react'
import routes from '../routes'

const components = {
  blog: props => <div>Blog</div>,
  blogPost: props => <div>Blog Post</div>
}

export default class extends React.Component {
  static async getInitialProps ({asPath}) {
    return routes.match(asPath)
  }

  render () {
    const {route, params, ...props} = this.props

    if (route) {
      const Component = components[route.name]
      Object.assign(props.url.query, params)
      return <Component {...props} />
    }

    return <div>Not found</div>
  }
}

The downsides with this method are no automatic code splitting and you need some custom error handling.

from next-routes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.