rsnext/examples/with-mobx-react-lite/README.md
Luis Alvarez D 1239d5af59
[Examples] Update mobx examples to not use SSR (#11576)
* Updated with-custom-reverse-proxy

* Updated readme of with-env-from-next-config-js

* Updated the kea example

* Updated with-mobx

* Updated with-mobx readme

* Updated the with-mobx-react-lite example
2020-04-07 09:48:13 -04:00

3.3 KiB

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.

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.

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).

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>
)