-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Examples] Update with-mobx-state-tree examples (#11736)
* Updated with-mobx-state-tree * Updated with-mobx-state-tree-typescript * minor fix
- Loading branch information
Luis Alvarez D
authored
Apr 8, 2020
1 parent
17384fc
commit 282f375
Showing
18 changed files
with
188 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,12 @@ | ||
import { Provider } from 'mobx-react' | ||
import { getSnapshot } from 'mobx-state-tree' | ||
import App from 'next/app' | ||
import React from 'react' | ||
import { initializeStore, IStore } from '../stores/store' | ||
import { useStore } from '../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 | ||
export default function App({ Component, pageProps }) { | ||
const store = useStore(pageProps.initialState) | ||
|
||
constructor(props) { | ||
super(props) | ||
this.store = initializeStore(props.isServer, props.initialState) as IStore | ||
} | ||
|
||
public render() { | ||
const { Component, pageProps } = this.props | ||
return ( | ||
<Provider store={this.store}> | ||
<Component {...pageProps} /> | ||
</Provider> | ||
) | ||
} | ||
return ( | ||
<Provider store={store}> | ||
<Component {...pageProps} /> | ||
</Provider> | ||
) | ||
} | ||
|
||
export default MyApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
import React from 'react' | ||
import { SampleComponent } from '../components/SampleComponent' | ||
import SampleComponent from '../components/SampleComponent' | ||
|
||
class IndexPage extends React.Component { | ||
public render() { | ||
return <SampleComponent title={'Index Page'} linkTo="/other" /> | ||
} | ||
export default () => { | ||
return <SampleComponent title="Index Page" linkTo="/other" /> | ||
} | ||
|
||
export default IndexPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
import React from 'react' | ||
import { SampleComponent } from '../components/SampleComponent' | ||
import SampleComponent from '../components/SampleComponent' | ||
|
||
class OtherPage extends React.Component { | ||
public render() { | ||
return <SampleComponent title={'Other Page'} linkTo="/" /> | ||
} | ||
export default () => { | ||
return <SampleComponent title={'Other Page'} linkTo="/" /> | ||
} | ||
|
||
export default OtherPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getSnapshot } from 'mobx-state-tree' | ||
import SampleComponent from '../components/SampleComponent' | ||
import { initializeStore } from '../store' | ||
|
||
export default () => { | ||
return <SampleComponent title={'SSG Page'} linkTo="/" /> | ||
} | ||
|
||
// If you build and start the app, the date returned here will have the same | ||
// value for all requests, as this method gets executed at build time. | ||
export function getStaticProps() { | ||
const store = initializeStore() | ||
|
||
store.update() | ||
|
||
return { props: { initialState: getSnapshot(store) } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getSnapshot } from 'mobx-state-tree' | ||
import SampleComponent from '../components/SampleComponent' | ||
import { initializeStore } from '../store' | ||
|
||
export default () => { | ||
return <SampleComponent title={'SSR Page'} linkTo="/" /> | ||
} | ||
|
||
// The date returned here will be different for every request that hits the page, | ||
// that is because the page becomes a serverless function instead of being statically | ||
// exported when you use `getServerSideProps` or `getInitialProps` | ||
export function getServerSideProps() { | ||
const store = initializeStore() | ||
|
||
store.update() | ||
|
||
return { props: { initialState: getSnapshot(store) } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,12 @@ | ||
import React from 'react' | ||
import { Provider } from 'mobx-react' | ||
import { getSnapshot } from 'mobx-state-tree' | ||
import App from 'next/app' | ||
import { initializeStore } from '../stores/store' | ||
import { useStore } from '../store' | ||
|
||
export default class MyApp extends App { | ||
static async getInitialProps({ Component, 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, | ||
} | ||
} | ||
export default function App({ Component, pageProps }) { | ||
const store = useStore(pageProps.initialState) | ||
|
||
constructor(props) { | ||
super(props) | ||
this.store = initializeStore(props.isServer, props.initialState) | ||
} | ||
|
||
render() { | ||
const { Component, pageProps } = this.props | ||
return ( | ||
<Provider store={this.store}> | ||
<Component {...pageProps} /> | ||
</Provider> | ||
) | ||
} | ||
return ( | ||
<Provider store={store}> | ||
<Component {...pageProps} /> | ||
</Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import React from 'react' | ||
import SampleComponent from '../components/SampleComponent' | ||
|
||
export default () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import React from 'react' | ||
import SampleComponent from '../components/SampleComponent' | ||
|
||
export default () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getSnapshot } from 'mobx-state-tree' | ||
import SampleComponent from '../components/SampleComponent' | ||
import { initializeStore } from '../store' | ||
|
||
export default () => { | ||
return <SampleComponent title={'SSG Page'} linkTo="/" /> | ||
} | ||
|
||
// If you build and start the app, the date returned here will have the same | ||
// value for all requests, as this method gets executed at build time. | ||
export function getStaticProps() { | ||
const store = initializeStore() | ||
|
||
store.update() | ||
|
||
return { props: { initialState: getSnapshot(store) } } | ||
} |
Oops, something went wrong.