SlideShare a Scribd company logo
MOBX FOR DUMMIES
YAUHENI NIKANOVICH
MobX is a simple, scalable and battle
tested state management solution
The philosophy behind MobX is very
simple: Anything that can be derived
from the application state, should be
derived. Automatically.
FLOW
REACT-MOBX
React and MobX together are a powerful combination.
‣ React renders the application state by providing mechanisms
to translate it into a tree of renderable components.
‣ MobX provides the mechanism to store and update the
application state that React then uses.
MobX adds observable capabilities to existing data structures like
objects, arrays and class instances.
Observable
ES NEXT ES5
Usage:
‣ observable(value)
‣ @observable classProperty = value
Observer
Observer function / decorator can be used to turn ReactJS
components into reactive components.
It wraps the component's render function in mobx.autorun to
make sure that any data that is used during the rendering of a
component forces a re-rendering upon change.
Observer is available through the separate mobx-react package.
Usage:
‣ observer(value)
‣ @observer class className { . . . }
Observer
STATELESS STATEFULL
Observer and store(s)
In order to bind a component and store(s) we can use Provider
component.
Provider allows to inject passed stores from the child components
by using props.
Observer adds lifecycle hook
When using mobx-react you can define a new life cycle hook,
componentWillReact that will be triggered when a component
will be scheduled to re-render because data it observes has
changed
‣ componentWillReact doesn't take arguments
‣ componentWillReact won't fire before the initial render

(use componentWillMount instead)
When to apply Observer
There is a simple rule: all components that render observable data
With @observer there is no need to distinguish 'smart'
components from 'dumb' components for the purpose of
rendering
It makes sure that whenever you start using observable data,
wrapped component will respond it
MobX-React DevTools
In combination with @observer you can use the MobX-React
DevTools, it shows exactly when your components are re-rendered,
also it shows data dependencies of your components
MobX-React DevTools
VISUALIZE COMPONENT RE-RENDER
SHOW COMPONENT DEPENDENCY TREE
LOG STATE CHANGES/REACTION
Characteristics of Observer component
‣ Observer only subscribe to the data structures that were actively
used during the last render. This means that you cannot under-
subscribe or over-subscribe. You can even use data in your rendering
that will only be available at later moment in time. This is ideal for
asynchronously loading data.
‣ You are not required to declare what data a component will use.
Instead, dependencies are determined at runtime and tracked in a
very fine-grained manner.
‣ Usually reactive components have no or little state, as it is often more
convenient to encapsulate (view) state in objects that are shared with
other component. But you are still free to use native state.
‣ @observer implements shouldComponentUpdate in the same way as
PureRenderMixin so that children are not re-rendered unnecessary.
‣ Reactive components sideways load data. Parent components won't
re-render unnecessarily even when child components will.
‣ @observer does not depend on React's context system.
Action
Any application has actions. Actions are anything that modify
the state. With MobX you can make it explicit in your code
where your actions live by marking them. Actions help you to
structure your code better.
Action is advised to use on any function that modifies
Observables or has side effects.
Action also provides useful debugging information in
combination with DevTools
Action using action is mandatory when strict mode is enabled
Action takes a function as argument and returns it after
wrapping it with transaction and allowStateChanges
Action
Usage:
‣ action(fn)
‣ action(name, fn)
‣ @action classMethod()
‣ @action(name) classMethod()
Transaction is a kind of scope of operations. Obserber will not
notified before transaction will not done
Note that transaction runs completely synchronously
Transactions can be nested. Only after completing the outer
transaction pending reactions will be run
Transaction
allowStateChanges
Can be used to (dis)allow state changes in a certain function.
Used internally by action to allow changes, and by computed
and observer to disallow state changes.
Strict mode
Enables / disables strict mode globally.
In strict mode, it is not allowed to change any state outside of
an action
Usage:
‣ useStrict(boolean)
Violation consequence:
Computed
With MobX you can define values that will be derived
automatically when relevant data is modified. By using the
@computed decorator
PropTypes
mobx-react provides the following additional PropTypes which can
be used to validate against MobX structures:
‣ observableArray
‣ observableArrayOf(React.PropTypes.number)
‣ observableMap
‣ observableObject
‣ arrayOrObservableArray
‣ arrayOrObservableArrayOf(React.PropTypes.number)
‣ objectOrObservableObject
Import PropTypes from mobx-react and use it:
‣ PropTypes.observableArray
PROS AND CONS
Pros:
‣ Makes your components reactive
‣ Easy to understand
‣ Friendly for users with

OOP background
‣ Less code
‣ Steep learning curve
‣ Responsive community
‣ ?? Magic ??
Cons:
?? Magic ??
Useful Links:
‣ Git repo: https://github.com/mobxjs/mobx
‣ Official Documentation: https://mobx.js.org/index.html
‣ MobX Creator on Medium: https://medium.com/@mweststrate
‣ Cheatsheet: http://ricostacruz.com/cheatsheets/mobx.html
‣ Boilerplates: https://mobx.js.org/faq/boilerplates.html
Q & A
YAUHENI NIKANOVICH
Thanks for attention
SLIDES: goo.gl/5qSjIt

EMAIL: zhenyanikon@gmail.com

More Related Content

PDF
React state management with Redux and MobX
PDF
Making react part of something greater
PDF
Designing applications with Redux
PDF
Using redux
PDF
The Road To Redux
PDF
Workshop 22: React-Redux Middleware
PDF
Redux js
PDF
State Models for React with Redux
React state management with Redux and MobX
Making react part of something greater
Designing applications with Redux
Using redux
The Road To Redux
Workshop 22: React-Redux Middleware
Redux js
State Models for React with Redux

What's hot (20)

PDF
How to Redux
PDF
Redux Universal
PPTX
React + Redux + TypeScript === ♥
PDF
Reactive Streams and RxJava2
PDF
Building React Applications with Redux
PDF
Reactive Thinking in Java with RxJava2
PPTX
ProvJS: Six Months of ReactJS and Redux
PPTX
Better React state management with Redux
PDF
An Overview of the React Ecosystem
PDF
Angular redux
PPTX
Getting started with Redux js
PPTX
React and Flux life cycle with JSX, React Router and Jest Unit Testing
PDF
Redux data flow with angular 2
PDF
Introduce Flux & react in practices (KKBOX)
PDF
React and redux
PDF
Workshop React.js
PDF
React.js+Redux Workshops
PDF
Let's Redux!
PDF
Data architectures in Angular & NGRX Introduction
PDF
Let's discover React and Redux with TypeScript
How to Redux
Redux Universal
React + Redux + TypeScript === ♥
Reactive Streams and RxJava2
Building React Applications with Redux
Reactive Thinking in Java with RxJava2
ProvJS: Six Months of ReactJS and Redux
Better React state management with Redux
An Overview of the React Ecosystem
Angular redux
Getting started with Redux js
React and Flux life cycle with JSX, React Router and Jest Unit Testing
Redux data flow with angular 2
Introduce Flux & react in practices (KKBOX)
React and redux
Workshop React.js
React.js+Redux Workshops
Let's Redux!
Data architectures in Angular & NGRX Introduction
Let's discover React and Redux with TypeScript
Ad

Similar to Mobx for Dummies - Yauheni Nikanowich - React Warsaw #5 (20)

PDF
MobX for dummies
PDF
Mob x in react
PPTX
PPTX
React-JS.pptx
PPTX
STATE MANAGEMENT IN REACT [Autosaved].pptx
PDF
100 React Interview questions 2024.pptx.pdf
PPTX
downloads_introduction to redux.pptx
PDF
Redux essentials
PPTX
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
PDF
React and MobX: A Truly Reactive App
PPTX
unit 2 Mastering-State-Management-in-React.pptx
PDF
Understanding React hooks | Walkingtree Technologies
PDF
Understanding react hooks
PPTX
an Introduction to Redux
PPTX
Redux workshop
PDF
Sagas Middleware Architecture
PPTX
FRONTEND BOOTCAMP 4.pptx
PPTX
Presentation on "An Introduction to ReactJS"
PDF
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
PPTX
Getting started with react & redux
MobX for dummies
Mob x in react
React-JS.pptx
STATE MANAGEMENT IN REACT [Autosaved].pptx
100 React Interview questions 2024.pptx.pdf
downloads_introduction to redux.pptx
Redux essentials
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React and MobX: A Truly Reactive App
unit 2 Mastering-State-Management-in-React.pptx
Understanding React hooks | Walkingtree Technologies
Understanding react hooks
an Introduction to Redux
Redux workshop
Sagas Middleware Architecture
FRONTEND BOOTCAMP 4.pptx
Presentation on "An Introduction to ReactJS"
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
Getting started with react & redux
Ad

Recently uploaded (20)

PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Nekopoi APK 2025 free lastest update
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
System and Network Administration Chapter 2
PDF
top salesforce developer skills in 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
AI in Product Development-omnex systems
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Transform Your Business with a Software ERP System
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
L1 - Introduction to python Backend.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Operating system designcfffgfgggggggvggggggggg
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Nekopoi APK 2025 free lastest update
Design an Analysis of Algorithms II-SECS-1021-03
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
System and Network Administration Chapter 2
top salesforce developer skills in 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
AI in Product Development-omnex systems
ManageIQ - Sprint 268 Review - Slide Deck
Transform Your Business with a Software ERP System
Odoo POS Development Services by CandidRoot Solutions
Wondershare Filmora 15 Crack With Activation Key [2025
How Creative Agencies Leverage Project Management Software.pdf
ISO 45001 Occupational Health and Safety Management System
2025 Textile ERP Trends: SAP, Odoo & Oracle
L1 - Introduction to python Backend.pptx

Mobx for Dummies - Yauheni Nikanowich - React Warsaw #5

  • 2. MobX is a simple, scalable and battle tested state management solution The philosophy behind MobX is very simple: Anything that can be derived from the application state, should be derived. Automatically.
  • 4. REACT-MOBX React and MobX together are a powerful combination. ‣ React renders the application state by providing mechanisms to translate it into a tree of renderable components. ‣ MobX provides the mechanism to store and update the application state that React then uses.
  • 5. MobX adds observable capabilities to existing data structures like objects, arrays and class instances. Observable ES NEXT ES5 Usage: ‣ observable(value) ‣ @observable classProperty = value
  • 6. Observer Observer function / decorator can be used to turn ReactJS components into reactive components. It wraps the component's render function in mobx.autorun to make sure that any data that is used during the rendering of a component forces a re-rendering upon change. Observer is available through the separate mobx-react package.
  • 7. Usage: ‣ observer(value) ‣ @observer class className { . . . } Observer STATELESS STATEFULL
  • 8. Observer and store(s) In order to bind a component and store(s) we can use Provider component. Provider allows to inject passed stores from the child components by using props.
  • 9. Observer adds lifecycle hook When using mobx-react you can define a new life cycle hook, componentWillReact that will be triggered when a component will be scheduled to re-render because data it observes has changed ‣ componentWillReact doesn't take arguments ‣ componentWillReact won't fire before the initial render
 (use componentWillMount instead)
  • 10. When to apply Observer There is a simple rule: all components that render observable data With @observer there is no need to distinguish 'smart' components from 'dumb' components for the purpose of rendering It makes sure that whenever you start using observable data, wrapped component will respond it
  • 11. MobX-React DevTools In combination with @observer you can use the MobX-React DevTools, it shows exactly when your components are re-rendered, also it shows data dependencies of your components
  • 12. MobX-React DevTools VISUALIZE COMPONENT RE-RENDER SHOW COMPONENT DEPENDENCY TREE LOG STATE CHANGES/REACTION
  • 13. Characteristics of Observer component ‣ Observer only subscribe to the data structures that were actively used during the last render. This means that you cannot under- subscribe or over-subscribe. You can even use data in your rendering that will only be available at later moment in time. This is ideal for asynchronously loading data. ‣ You are not required to declare what data a component will use. Instead, dependencies are determined at runtime and tracked in a very fine-grained manner. ‣ Usually reactive components have no or little state, as it is often more convenient to encapsulate (view) state in objects that are shared with other component. But you are still free to use native state. ‣ @observer implements shouldComponentUpdate in the same way as PureRenderMixin so that children are not re-rendered unnecessary. ‣ Reactive components sideways load data. Parent components won't re-render unnecessarily even when child components will. ‣ @observer does not depend on React's context system.
  • 14. Action Any application has actions. Actions are anything that modify the state. With MobX you can make it explicit in your code where your actions live by marking them. Actions help you to structure your code better. Action is advised to use on any function that modifies Observables or has side effects. Action also provides useful debugging information in combination with DevTools Action using action is mandatory when strict mode is enabled
  • 15. Action takes a function as argument and returns it after wrapping it with transaction and allowStateChanges Action Usage: ‣ action(fn) ‣ action(name, fn) ‣ @action classMethod() ‣ @action(name) classMethod()
  • 16. Transaction is a kind of scope of operations. Obserber will not notified before transaction will not done Note that transaction runs completely synchronously Transactions can be nested. Only after completing the outer transaction pending reactions will be run Transaction allowStateChanges Can be used to (dis)allow state changes in a certain function. Used internally by action to allow changes, and by computed and observer to disallow state changes.
  • 17. Strict mode Enables / disables strict mode globally. In strict mode, it is not allowed to change any state outside of an action Usage: ‣ useStrict(boolean) Violation consequence:
  • 18. Computed With MobX you can define values that will be derived automatically when relevant data is modified. By using the @computed decorator
  • 19. PropTypes mobx-react provides the following additional PropTypes which can be used to validate against MobX structures: ‣ observableArray ‣ observableArrayOf(React.PropTypes.number) ‣ observableMap ‣ observableObject ‣ arrayOrObservableArray ‣ arrayOrObservableArrayOf(React.PropTypes.number) ‣ objectOrObservableObject Import PropTypes from mobx-react and use it: ‣ PropTypes.observableArray
  • 20. PROS AND CONS Pros: ‣ Makes your components reactive ‣ Easy to understand ‣ Friendly for users with
 OOP background ‣ Less code ‣ Steep learning curve ‣ Responsive community ‣ ?? Magic ?? Cons: ?? Magic ??
  • 21. Useful Links: ‣ Git repo: https://github.com/mobxjs/mobx ‣ Official Documentation: https://mobx.js.org/index.html ‣ MobX Creator on Medium: https://medium.com/@mweststrate ‣ Cheatsheet: http://ricostacruz.com/cheatsheets/mobx.html ‣ Boilerplates: https://mobx.js.org/faq/boilerplates.html Q & A
  • 22. YAUHENI NIKANOVICH Thanks for attention SLIDES: goo.gl/5qSjIt
 EMAIL: zhenyanikon@gmail.com