rimraf
rimraf

Reputation: 4126

mobx store not accessable inside lifecycle methods?

I have a react component which i am injecting a mobx store. when I try to access an observable from my store inside a lifecycle method (componentWillMount or componentDidMOunt) the value is empty. however when I access it inside the render method it is available. Is there something I am missing? I feel like I'm missing something basic here.

Upvotes: 2

Views: 1326

Answers (2)

Juraj
Juraj

Reputation: 260

Hi by my experience with Mobx state management you should not access the

observalbe variables from React Component. Even when you inject the Redux Store into React Component. (Decorate or wrap).

The computed variables are designed to provide updated observable variables to React Component.

You can access Mobx actions and computed but not the observable. They are used only inside Mobx class.

Upvotes: 0

Konstantin
Konstantin

Reputation: 25339

The documentation clearly states that observer from mobx-react tracks observables only in render method of wrapped component.

Function (and decorator) that converts a React component definition, React component class or stand-alone render function into a reactive component, which tracks which observables are used by render and automatically re-renders the component when one of these values changes.

If you take a look at observer sources you can confirm that it only tracks render method

Upvotes: 3

Related Questions