question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MobX Stores are not Reactive in Next.js v9 when using Hooks

See original GitHub issue

next.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:

  1. Create a new next.js app - yarn create next-app
  2. Add mobx and mobx-react
  3. Create a simple store file and export default createContext(new Store())
  4. In a function component const store = useContext(ImportedStore)
  5. Display some items in a list from the store
  6. Add a button that attempt to set the items to an empty array
  7. No re-render is triggered

Sample Project

Download next-mobx.zip

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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
danielkczcommented, Aug 6, 2019

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?

4reactions
danielkczcommented, Jul 15, 2019

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! ✨

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found