Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created November 25, 2020 11:10
Show Gist options
  • Select an option

  • Save sibelius/d86641674909d51a5520590238b54a1b to your computer and use it in GitHub Desktop.

Select an option

Save sibelius/d86641674909d51a5520590238b54a1b to your computer and use it in GitHub Desktop.
NextRouter Provider to be used on Storybook
import { RouterContext } from 'next/dist/next-server/lib/router-context'
import Router from 'next/router'
import React from 'react'
Router.router = {
route: '/',
pathname: '/',
query: {},
asPath: '/',
push(url, as, options) {
// action('nextRouter.push')(url, as, options);
return Promise.resolve(true)
},
replace(url, as, options) {
// action('nextRouter.replace')(url, as, options);
return Promise.resolve(true)
},
reload() {
// action('nextRouter.reload')();
},
back() {
// action('nextRouter.back')();
},
prefetch(url, asPath, options) {
// action('nextRouter.prefetch')(url, asPath, options);
return Promise.resolve()
},
beforePopState(cb) {
// action('nextRouter.beforePopState')(cb);
},
events: {
on(type, handler) {
// action('nextRouter.events.on')(type, handler);
},
off(type, handler) {
// action('nextRouter.events.off')(type, handler);
},
emit(type) {
// action('nextRouter.events.emit')(type);
},
},
}
export const StorybookNextRouter = ({ children }) => {
return <RouterContext.Provider value={Router.router}>{children}</RouterContext.Provider>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment