{"id":18314320,"url":"https://github.com/agrublev/with-mobx-state-tree","last_synced_at":"2025-04-09T12:41:31.086Z","repository":{"id":40918049,"uuid":"225185553","full_name":"agrublev/with-mobx-state-tree","owner":"agrublev","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-05T02:04:00.000Z","size":9466,"stargazers_count":0,"open_issues_count":20,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-15T06:41:27.170Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://vuepress-eight-tawny.now.sh","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/agrublev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-01T15:42:04.000Z","updated_at":"2019-12-01T15:49:17.000Z","dependencies_parsed_at":"2023-02-03T05:16:21.903Z","dependency_job_id":null,"html_url":"https://github.com/agrublev/with-mobx-state-tree","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agrublev%2Fwith-mobx-state-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agrublev%2Fwith-mobx-state-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agrublev%2Fwith-mobx-state-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agrublev%2Fwith-mobx-state-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/agrublev","download_url":"https://codeload.github.com/agrublev/with-mobx-state-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045220,"owners_count":21038552,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T16:31:47.951Z","updated_at":"2025-04-09T12:41:31.067Z","avatar_url":"https://github.com/agrublev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n# MobX State Tree example\n\n## How to use\n\n### Using `create-next-app`\n\nExecute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:\n\n```bash\nnpx create-next-app --example with-mobx-state-tree with-mobx-state-tree-app\n# or\nyarn create next-app --example with-mobx-state-tree with-mobx-state-tree-app\n```\n\n### Download manually\n\nDownload the example:\n```bash\ncurl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-mobx-state-tree\ncd with-mobx-state-tree\n```\nInstall it and run:\n\n```bash\nnpm install\nnpm run dev\n# or\nyarn\nyarn dev\n```\n\nDeploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))\n\n```bash\nnow\n```\n\n## Notes\n\nThis example is a mobx port of the [with-redux](https://github.com/zeit/next.js/tree/master/examples/with-redux) example. Decorator support is activated by adding a `.babelrc` file at the root of the project:\n\n```json\n{\n  \"presets\": [\"next/babel\"],\n  \"plugins\": [\"transform-decorators-legacy\"]\n}\n```\n\n### Rehydrating with server data\n\nAfter initializing the store (and possibly making changes such as fetching data), `getInitialProps` must stringify the store in order to pass it as props to the client. `mobx-state-tree` comes out of the box with a handy method for doing this called `getSnapshot`. The snapshot is sent to the client as `props.initialState` where the pages's `constructor()` may use it to rehydrate the client store.\n\n## The idea behind the example\n\nUsually 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.\n\nIn 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.\n\n![](http://i.imgur.com/JCxtWSj.gif)\n\nOur 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-state-tree store and returning the initial timestamp to be rendered. The root component for the render method is the `mobx-react \u003cProvider\u003e` that allows us to send the store down to children components so they can access to the state when required.\n\nTo 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.\n\nThe trick here for supporting universal mobx is to separate the cases for the client and the server. When we are on the server we want to create a new store every time, otherwise different users data will be mixed up. If we are in the client we want to use always the same store. That's what we accomplish on `store.js`\n\nThe clock, under `components/Clock.js`, has access to the state using the `inject` and `observer` functions from `mobx-react`. In this case Clock is a direct child from the page but it could be deep down the render tree.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagrublev%2Fwith-mobx-state-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fagrublev%2Fwith-mobx-state-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagrublev%2Fwith-mobx-state-tree/lists"}