1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-mobx-state-tree-typescript/pages/_app.tsx
Don Alvarez af9214b302 mobx-state-tree examples should use _app (#5362)
The mobx-state-tree examples (with and without typescript) pre-dated the next _app class and needed to be updated to use _app for persisting state across client-side navigation transitions. Also removed unneeded custom server class to better keep with the "show one feature per example" style of the next examples folder
2018-10-14 10:04:58 +02:00

56 lines
1.3 KiB
TypeScript

import { Provider } from "mobx-react";
import { getSnapshot } from "mobx-state-tree";
import App, { Container } from "next/app";
import React from "react";
import { initializeStore, IStore } from "../stores/store";
interface IOwnProps {
isServer:boolean;
initialState:IStore;
}
class MyApp extends App {
public static async getInitialProps({ Component, router, ctx }) {
//
// Use getInitialProps as a step in the lifecycle when
// we can initialize our store
//
const isServer = (typeof window === "undefined");
const store = initializeStore(isServer);
//
// Check whether the page being rendered by the App has a
// static getInitialProps method and if so call it
//
let pageProps = {};
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx);
}
return {
initialState: getSnapshot(store),
isServer,
pageProps,
};
}
private store:IStore;
constructor(props) {
super(props);
this.store = initializeStore(props.isServer, props.initialState) as IStore;
}
public render() {
const { Component, pageProps } = this.props;
return (
<Container>
<Provider store={this.store}>
<Component {...pageProps} />
</Provider>
</Container>
);
}
}
export default MyApp;