- Introduction
- Introduction to MobX
- 2. Core API
- Advanced API
- Best Practices For Large Apps
- How does MobX work?
- Tips & Tricks
- 6.1. DevTools
- 6.2. ES5 / ES6 / TypeScript syntax
- 6.3. Stateless Components and Hot Module Reloading
- 6.4. Tracking state changes
- 6.5. Performance considerations
- 7. Resources
- 8. Frequently Asked Questions
- Published with GitBook
MobX
Organizing React Components
See also @observer
@observer
is quite similar to decorators like @connect
as found in several flux libraries, yet there are some important differences.
@observer
needs no configuration; dependencies will be detected automatically.- The subscriptions and updates of
@observer
are far more fine-grained; exactly those values used in the last rendering will be observed, so there is no risk of over- or undersubscription. Large applications really benefit from this in terms of simplicity performance. - This mechanism does not depend on the concept of context.
Rule of thumb is to use observer
on every component in your application that is specific for your application. With @observer
there is no need to distinguish 'smart' components from 'dumb' components. All components become responsible for updating when their own dependencies change. Its overhead is neglectable and it makes sure that whenever you start using observable data the component will respond to it.
Since in practice you will see that most reactive components become stateless, they can easily be hot-reloaded. You will discover that many small components will consist of just a render function. In such cases, you can also directly pass the render function to observer
, without building a component. The props will then be available as first argument of the function.
observer
also prevents re-renderings when the props of the component have only shallowly changed, which makes a lot of sense if the data passed into the component is reactive. This behavior is similar to React PureRender mixin, except that state changes are still always processed. If a component provides its own shouldComponentUpdate
, that one takes precedence.