MobX Stores are not Reactive in Next.js v9 when using Hooks
See original GitHub issuenext.js / mobx bug report
Example name
with-mobx
Describe the bug
MobX stores are not reactive in Next.js v9 when using hooks
"mobx": "^5.11.0",
"mobx-react": "^6.1.1",
"next": "^9.0.1",
"react": "^16.8.6",
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Create a new next.js app -
yarn create next-app
- Add
mobx
andmobx-react
- Create a simple store file and
export default createContext(new Store())
- In a function component
const store = useContext(ImportedStore)
- Display some items in a list from the store
- Add a button that attempt to set the items to an empty array
- No re-render is triggered
Sample Project
Expected behavior
A re-render should have been triggered by MobX
Additional context
This bare bones example works fine when using create-react-app instead of create-next-app
PostsStore.js
import { createContext } from 'react'
import { action, decorate, observable } from 'mobx'
export class PostsStore {
items = [
'Foo',
'Bar'
]
reset() {
this.items = ['Reset']
}
}
decorate(PostsStore, {
items: observable,
reset: action
})
export default createContext(new PostsStore())
pages/index.js
import React, { useContext } from 'react'
import { observer } from 'mobx-react'
import PostsStore from '../stores/posts'
const Home = () => {
const store = useContext(PostsStore)
return (
<div>
{store.items.map(item => (<p key={item}>{item}</p>))}
<p>
{/* Clicking reset() DOES NOT trigger a re-render */}
<button onClick={() => store.reset()}>Reset</button>
</p>
</div>
)
}
export default observer(Home)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:22 (2 by maintainers)
Top Results From Across the Web
MobX Strategies with React Hooks - Medium
Here are few implementation techniques for using MobX with react hooks. If you have used redux or react contexts, these should be fairly...
Read more >Mobx Server Side Rendering with Next.js - DEV Community
In this article, we are going to use Mobx with the root store pattern and Next.js framework to do a server-side rendering of...
Read more >MobX not hydrating in next.js state when fetching async data
It's does not handle async stuff, so you can't really wait until the data is loaded; Store is created both on the server...
Read more >9.0.3 - next - npm
Notice that to load data when the page loads, we use getInitialProps which is an async static method. It can asynchronously fetch anything...
Read more >Getting Started With React
React Tutorial – Learn React and JavaScript Programming Basics with Example Code (freecodecamp.org). Mar 01, 2021. If you are a developer getting started ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t really see any benefit of your package against regular React Context. It’s kinda funny you actually want a donation for that 😄 Hard pass.
Besides, how is it related to this issue?
I don’t use classes at all, they are so messy 🙊 Decorators are just too weird or maybe I am weird, who knows 😃 Hooks forever! ✨