Mobx: Can i use Mobx observe stateless component in react ?

Created on 24 Feb 2017  Â·  10Comments  Â·  Source: mobxjs/mobx

Can i use Mobx observe stateless component in react ?

Most helpful comment

Dropping this snippet here for myself next time I have to Google this...

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

const Vegetable = ({ FruitStore }) => (
  <h1>{FruitStore.favorite}</h1>
)

export default inject('FruitStore')(observer(Vegetable))

All 10 comments

You can use a React stateless function component with MobX if that's what you mean? See the Stateless Function Components section here:
https://mobx.js.org/refguide/observer-component.html

@jamiewinder oh i will try.

thank you

Just try:

const App = observer(({ name }) => (<span>hello {name}</span>))

*observer:

const App = observer(({ name }) => (<span>hello {name}</span>))

Hi,
I would like to know if using a stateless component has any optimizations when used with observer? Mobx is managing state. So , if I use stateless component with observer, it still tracks state in some form right?
So what is better

@observer
export class SomeComponent extends React.Component<{ value: string }, {}> {
public render() {
return (


);
}
}

OR

export const widgetSummaryLabel = observer(({ labelValue: string }) =>

{}


);

Thank i got it.

Dropping this snippet here for myself next time I have to Google this...

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

const Vegetable = ({ FruitStore }) => (
  <h1>{FruitStore.favorite}</h1>
)

export default inject('FruitStore')(observer(Vegetable))

This will not work if we use Hooks inside of our functions. Is there an workaround that I do not know of?

Please don't comment on old issues but open new ones instead

Op do 30 mei 2019 12:53 schreef Radu Micu notifications@github.com:

This will not work if we use Hooks inside of our functions. Is there an
workaround that I do not know of?

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx/issues/853?email_source=notifications&email_token=AAN4NBAM42DUSXDLE2RXSLDPX6W2XA5CNFSM4DBOLPFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWSAY5A#issuecomment-497290356,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAN4NBF3V5GHUFJ3ZLPA2OTPX6W2XANCNFSM4DBOLPFA
.

There's a new way to do this @corysimmons - posting here in case anybody finds this thread:
🔗 mobx-react official migration to hooks guide

Was this page helpful?
0 / 5 - 0 ratings