rsnext/examples/with-mobx-react-lite
Joe Haddad 18a9c7e371
Improve linting rules to catch more errors (#9374)
* Update `packages/`

* Update examples

* Update tests

* Update bench

* Update top level files

* Fix build

* trigger
2019-11-10 19:24:53 -08: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 Replace the deprecated Create Next App URL (#9032) 2019-10-10 23:34:14 -04:00
store.js Improve linting rules to catch more errors (#9374) 2019-11-10 19:24:53 -08:00

MobX example

How to use

Using create-next-app

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

npx create-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 now (download)

now

Notes

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

The idea behind the 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.

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