Skip to content

Commit 8279575

Browse files
committedAug 28, 2018
▲ Minimal Next.js Cosmos proxy
Inspired by react-cosmos/react-cosmos#766
1 parent 39457c2 commit 8279575

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
 

‎cosmos.proxies.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import Router from 'next/router';
3+
4+
class NextJSProxy extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
8+
this.__realRouter = Router.router;
9+
Router.router = createRouterMock();
10+
}
11+
12+
componentWillUnmount() {
13+
if (this.__realRouter) {
14+
Router.router = this.__realRouter;
15+
}
16+
}
17+
18+
render() {
19+
const { nextProxy, ...rest } = this.props;
20+
const { value: NextProxy, next } = nextProxy;
21+
22+
return <NextProxy {...rest} nextProxy={next()} />;
23+
}
24+
}
25+
26+
function createRouterMock() {
27+
return {
28+
push: () => {},
29+
prefetch: () => {},
30+
replace: () => {},
31+
};
32+
}
33+
34+
export default [NextJSProxy];

0 commit comments

Comments
 (0)
Please sign in to comment.