-
Notifications
You must be signed in to change notification settings - Fork 27.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Singleton access to router #267
Comments
absolutely. I've been thinking we could do import Link from 'next/link'
Link.go('/a') as an alternative to |
I've created a quick PR for this (#275) but I'm sure there is a better solution. Also, I think it makes more sense to put expose the Router and have the 'go' method on the router instead of the Link. |
I also feel singleton |
I am down with exposing a Router to the user too. It sounds better than |
I have a working solution but I'm not happy with storing the current router's instance to (/lib/router.js) export default class Router {
constructor (url, initialData) {
const parsed = parse(url, true)
// represents the current component key
this.route = toRoute(parsed.pathname)
// set up the component cache (by route keys)
this.components = { [this.route]: initialData }
this.pathname = parsed.pathname
this.query = parsed.query
this.subscriptions = new Set()
this.componentLoadCancel = null
this.onPopState = this.onPopState.bind(this)
if (typeof window !== 'undefined') {
window.addEventListener('popstate', this.onPopState)
-> window.currentRouter = this // <---- save class instance
}
}
/* expose method statically so we can use
import Router from 'next/router'
Router.go('/path')
*/
static go (href) {
window.currentRouter.change('pushState', null, href)
}
.... I've also tried saving the router as a variable in router.js but apparently you can't access those from the constructor. Any other suggestions? |
@jonaswindey I think this is only way to fix it until we land a fix for #253 |
Ok, do I already create a PR for this so we can proceed? |
We already expose https://github.com/zeit/next.js/blob/master/client/next-dev.js |
Stats from current PRDefault Server ModeGeneral
Client Bundles (main, webpack, commons)
Client Bundles (main, webpack, commons) Modern
Client Pages
Client Pages Modern
Client Build Manifests
Rendered Page Sizes
Serverless ModeGeneral
Client Bundles (main, webpack, commons)
Client Bundles (main, webpack, commons) Modern
Client Pages
Client Pages Modern
Client Build Manifests
Serverless bundles
Commit: 3f51f0e |
There should be a way to access the router instance globally.
Right now, if one wants to use a redirect from an action or event, we have to provide the router in the component's
static contextTypes
and redirect withthis.context.router.push(null, 'page')
(or pass the router to the redux action)However, if we want to do redirects in redux actions or other helpers, it would be great to have a global access to this router instance. React-router solves this by using history.push() from the (browser)history package.
What would be the best way to implement this?
The text was updated successfully, but these errors were encountered: