Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Using observer with react hooks does not work #594

Closed
barbalex opened this issue Oct 31, 2018 · 13 comments
Closed

Using observer with react hooks does not work #594

barbalex opened this issue Oct 31, 2018 · 13 comments

Comments

@barbalex
Copy link

https://codesandbox.io/s/7z9yok8mn1

This error occurs:

Hooks can only be called inside the body of a function component.

There is already an issue for this (#591) but it was closed as the author wants to wait for a version of mobx-react that uses hooks itself.

Although I am also looking forward to that my intention is to try out hooks in my projects and start using them, even if mobx-react itself can not yet be switched.

This is not possible as observer causes the above error.

@barbalex
Copy link
Author

barbalex commented Oct 31, 2018

Looking at the code (https://github.com/mobxjs/mobx-react/blob/master/src/observer.js#L321-L343) I get the feeling that observer is converting a function component to a class which will not work when using hooks?

@barbalex
Copy link
Author

So I guess we will have to wait for #588 to be able to use hooks together with mobx.

@johot
Copy link

johot commented Oct 31, 2018

I have a working prototype now that fixes the error you are seeing (see #588). https://github.com/johot/mobx-react

@mweststrate
Copy link
Member

Closing in favor of #588. We won't be actively supporting a feature that is not actually released.

@mweststrate
Copy link
Member

The simple work around is to use <Observer> sections in your fucntion components, instead of observer wrapper around it

@barbalex
Copy link
Author

barbalex commented Nov 6, 2018

@mweststrate I did not know about <Observer>. Thanks for the tip.

@davidtran
Copy link

Please give me an example.

@mweststrate
Copy link
Member

mweststrate commented Jan 11, 2019 via email

@mweststrate
Copy link
Member

@davidtran observer((props) => rendering) is the same as (props) => <Observer>{() => rendering}</Observer>, but the latter has the benefit that it doesn't turn your function component into a class component.

@barbalex
Copy link
Author

You can now also use observer from mobx-react-lite

@ali-master
Copy link
Contributor

Hi guys.

I did your tips but the problem is still yet. May someone help me to fix it?
I'm using the latest version of React and mobx and the class way of mobx for managing stores and when I call an action function, everything looks good and the store will be updated but DOM can't update.

Thank you in advance and I appreciate your help.

@mweststrate
Copy link
Member

@ali-master please open a fresh issue, following the normal format with reproduction and such.

@ali-master
Copy link
Contributor

Thank you, I will

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants