next-mobx-wrapper
Mobx wrapper for Next.js
⚠️ This will work only with Next.js 6+ ⚠️
examples/with-mobx-wrapper
Example:Installation
$ yarn add next-mobx-wrapper
Usage
withMobx
into _app.js
Step 1: Wrap // pages/_app.js ;;; ; const isServer = !processbrowser; ;; // NOT `true` value { const Component pageProps store = thisprops; return <Container> <Provider ...store> <Component ...pageProps /> </Provider> </Container> ; } getStoresMyApp;
Step 2: Create the stores
- Create
userStore
sample:
// stores/user.js ;;; @observable userRegistry = ; fetchUser = ; { return thisuserRegistry; }; // Make sure the store’s unique name// AND must be same formula// Example: getUserStore => userStore// Example: getProductStore => productStoreconst getUserStore = ;
- Create the
rootStore
:
// stores/index.js ;;
Step 3: Binding data
- Any pages
// pages/user.js Component static async { const id = query; await userStore; const user = userStore; return user ; } { const user = thisprops; return <div>Username: username</div>; } ;
- Or any components
// components/UserInfo.jsx ; @Component { return <div>Username: thispropsusername</div>; } // Somewhere<SampleThing> <UserInfo id=9></SampleThing>
- Or with Hooks
// components/UserInfo.jsx ;; const UserInfo = { const userStore: getUserById = ; const user = ; return <div>Username: username</div>;}; ;
Next.js 8
you need add more, if you want to use Observable Maps
Note: //.babelrc
API
;
License
MIT © Nghiep