Comments (3)
I didn't try this yet but here is some information that might be helpful: vercel/next.js#88
from next-routes.
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.
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)
- Click on link and URL update is not working HOT 1
- Complex { Link } from routes module doesn't work when coming from static pages, but does when navigated to first HOT 5
- next 9 with next-routes and public folder not working
- Route pushRouter makes app refresh
- Empty value for route
- Nextjs v9 - Parameter 'url' must be a string, not undefined HOT 1
- Is this library alive? HOT 22
- Refresh results in 404 page not found HOT 4
- Bug with getServerSideProps HOT 2
- Compatibility with getStaticPaths HOT 1
- Error using Link component "Warning: Function components cannot be given refs." HOT 1
- Failed to decode downloaded font
- slugs are not working with dynamic routes after upgrading to next 9.5 HOT 2
- assigning the index page to multiple routes leads to infinite reloading HOT 1
- Page Both Accessible From Route Url And NextPage Name HOT 1
- Next 10 HOT 2
- React 17.02, next js 11 HOT 2
- Migrating from next-routes to built-in Next.js routing? HOT 1
- vercel deployment with this lib
- Migration steps for nextjs inbuilt routing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-routes.