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.

hooks not plays well with mobx-react observer

See original GitHub issue

Do you want to request a feature or report a bug? bug

What is the current behavior?

My code is shown below, it complains ‘Hooks can only be called inside the body of a function component.’

import React, { useState } from 'react'
import { observer } from 'mobx-react'

import WishListItemEdit from './WishListItemEdit'

const WishListItemView = ({ item }) => {
    const [isEditing, setIsEditing] = useState(false)
    const toggleEditing = () => setIsEditing(!isEditing)

    if (isEditing) {
        return (
            <WishListItemEdit item={item} />
        )
    }

    return (
        <li className="item">
            { item.image && <img src={item.image} /> }
            <h3>{item.name}</h3>
            <span>{item.price}</span>
            <span>
                <button onClick={toggleEditing}>Edit</button>
            </span>
        </li>
    )
}

export default observer(WishListItemView)

after removing the observer, using export default WishListItemView, it works. Is there a conflict between using mobx observer with react hooks? Since the component is no longer an observer component following the mobx pattern.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

I created my repo from CRA, and update both react and react-dom to 16.7-alpha.0

What is the expected behavior?

it works well with mobx-react observer

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
philipp-spiesscommented, Oct 29, 2018

@kimochg Got it! Looks like there’s an upstream issue at the mobxjs repo for this: https://github.com/mobxjs/mobx-react/issues/591.

0reactions
elie222commented, Feb 6, 2019

@kimochg did you find a way to get React hooks and observer working nicely together?

Read more comments on GitHub >

github_iconTop Results From Across the Web

MobX React: Define a observer hook - Stack Overflow
I'm trying to define my own hooks in a MobX project which depend on mobx observables. But it's not possible to wrap a...
Read more >
MobX Strategies with React Hooks - Medium
We need to wrap our component with observer to keep track of changes in observables so that our component gets notified on state...
Read more >
React integration - MobX
Hint: you can play with the above example yourself on CodeSandbox. The observer HoC automatically subscribes React components to any observables that are...
Read more >
Using MobX with React Hooks - DEV Community ‍ ‍
This post assumes familiarity with MobX. Hooks have drastically changed the way we can write functional components in React.
Read more >
How to use Mobx with React Hooks - YouTube
Learn how to use Mobx with React Hooks.Code: https://github.com/benawad/ react -native-web-series/tree/3_mobxPlaylist: ...
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