rsnext/examples/with-mobx-react-lite
Shu Uesugi 5f041447bc
Remove Now CLI reference from examples (#10501)
* Find/Replace "Deploy it to the cloud..."

* Find/Replace "Deploy it to the cloud..." (no colon)

* Find/Replace "Deploy it to the cloud..." for firebase

* Convert remaining ones

* Storybook deployment

* Update with-stripe-typescript

* Update contributing.md

* Remove `now`

* Update examples/with-stripe-typescript/README.md

Co-Authored-By: Luis Alvarez D. <luis@zeit.co>
2020-02-12 17:14:57 -05:00
..
components Improve linting rules to catch more errors (#9374) 2019-11-10 19:24:53 -08:00
pages Improve linting rules to catch more errors (#9374) 2019-11-10 19:24:53 -08:00
.babelrc Port with-mobx to with-mobx-react-lite (#6617) 2019-03-16 16:47:28 +01:00
package.json Port with-mobx to with-mobx-react-lite (#6617) 2019-03-16 16:47:28 +01:00
README.md Remove Now CLI reference from examples (#10501) 2020-02-12 17:14:57 -05:00
store.js Improve linting rules to catch more errors (#9374) 2019-11-10 19:24:53 -08:00

MobX example

Usually splitting your app state into pages feels natural but sometimes you'll want to have global state for your app. This is an example on how you can use MobX that also works with our universal rendering approach. This is just a way you can do it but it's not the only one.

Deploy your own

Deploy the example using ZEIT Now:

Deploy with ZEIT Now

How to use

Using create-next-app

Execute create-next-app with npm or Yarn to bootstrap the example:

npm init next-app --example with-mobx-react-lite with-mobx-react-lite-app
# or
yarn create next-app --example with-mobx-react-lite with-mobx-react-lite-app

Download manually

Download the example:

curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-mobx-react-lite
cd with-mobx-react-lite

Install it and run:

npm install
npm run dev
# or
yarn
yarn dev

Deploy it to the cloud with ZEIT Now (Documentation).

Notes

In this example we are going to display a digital clock that updates every second. The first render is happening in the server and then the browser will take over. To illustrate this, the server rendered clock will have a different background color than the client one.

This example is a mobx-react-lite port of the with-mobx example. MobX support has been implemented using React Hooks.

Our page is located at pages/index.js so it will map the route /. To get the initial data for rendering we are implementing the static method getInitialProps, initializing the MobX store and returning the initial timestamp to be rendered. The root component for the render method is a React context provider that allows us to send the store down to children components so they can access to the state when required.

To pass the initial timestamp from the server to the client we pass it as a prop called lastUpdate so then it's available when the client takes over.

Inplementation details

The initial store data is returned from the initializeData function that recycles existing store data if it already exists.

function initializeData(initialData = store || {}) {
  const { lastUpdate = Date.now(), light } = initialData
  return {
    lastUpdate,
    light: Boolean(light),
  }
}

The observable store is created in a function component by passing a plain JavaScript object to the useObservable hook. Actions on the observable store (start and stop) are created in the same scope as the store in store.js and exported as named exports.

store = useObservable(initializeData(props.initialData))

start = useCallback(
  action(() => {
    // Async operation that mutates the store
  })
)

stop = () => {
  // Does not mutate the store
}

The component creates and exports a new React context provider that will make the store accessible to all of its descendents.

return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>

The store is accessible at any depth by using the StoreContext.

const store = useContext(StoreContext)

The clock, under components/Clock.js, reacts to changes in the observable store by means of the useObserver hook.

return (
  <div>
    // ...
    {useObserver(() => (
      <Clock lastUpdate={store.lastUpdate} light={store.light} />
    ))}
    // ...
  </div>
)