SlideShare une entreprise Scribd logo
1  sur  22
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥Remo Jansen
 
Reactive Streams and RxJava2
Reactive Streams and RxJava2Reactive Streams and RxJava2
Reactive Streams and RxJava2Yakov Fain
 
Building React Applications with Redux
Building React Applications with ReduxBuilding React Applications with Redux
Building React Applications with ReduxFITC
 
Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Yakov Fain
 
ProvJS: Six Months of ReactJS and Redux
ProvJS:  Six Months of ReactJS and ReduxProvJS:  Six Months of ReactJS and Redux
ProvJS: Six Months of ReactJS and ReduxThom Nichols
 
Better React state management with Redux
Better React state management with ReduxBetter React state management with Redux
Better React state management with ReduxMaurice De Beijer [MVP]
 
An Overview of the React Ecosystem
An Overview of the React EcosystemAn Overview of the React Ecosystem
An Overview of the React EcosystemFITC
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux jsCitrix
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React and  Flux life cycle with JSX, React Router and Jest Unit TestingReact and  Flux life cycle with JSX, React Router and Jest Unit Testing
React and Flux life cycle with JSX, React Router and Jest Unit TestingEswara Kumar Palakollu
 
Redux data flow with angular 2
Redux data flow with angular 2Redux data flow with angular 2
Redux data flow with angular 2Gil Fink
 
Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Hsuan Fu Lien
 
Data architectures in Angular & NGRX Introduction
Data architectures in Angular & NGRX IntroductionData architectures in Angular & NGRX Introduction
Data architectures in Angular & NGRX IntroductionFabio Biondi
 
Let's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScriptLet's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScriptMathieu Savy
 

Tendances (20)

How to Redux
How to ReduxHow to Redux
How to Redux
 
Redux Universal
Redux UniversalRedux Universal
Redux Universal
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Reactive Streams and RxJava2
Reactive Streams and RxJava2Reactive Streams and RxJava2
Reactive Streams and RxJava2
 
Building React Applications with Redux
Building React Applications with ReduxBuilding React Applications with Redux
Building React Applications with Redux
 
Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2
 
ProvJS: Six Months of ReactJS and Redux
ProvJS:  Six Months of ReactJS and ReduxProvJS:  Six Months of ReactJS and Redux
ProvJS: Six Months of ReactJS and Redux
 
Better React state management with Redux
Better React state management with ReduxBetter React state management with Redux
Better React state management with Redux
 
An Overview of the React Ecosystem
An Overview of the React EcosystemAn Overview of the React Ecosystem
An Overview of the React Ecosystem
 
Angular redux
Angular reduxAngular redux
Angular redux
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React and  Flux life cycle with JSX, React Router and Jest Unit TestingReact and  Flux life cycle with JSX, React Router and Jest Unit Testing
React and Flux life cycle with JSX, React Router and Jest Unit Testing
 
Redux data flow with angular 2
Redux data flow with angular 2Redux data flow with angular 2
Redux data flow with angular 2
 
Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)
 
React and redux
React and reduxReact and redux
React and redux
 
Workshop React.js
Workshop React.jsWorkshop React.js
Workshop React.js
 
React.js+Redux Workshops
React.js+Redux WorkshopsReact.js+Redux Workshops
React.js+Redux Workshops
 
Let's Redux!
Let's Redux!Let's Redux!
Let's Redux!
 
Data architectures in Angular & NGRX Introduction
Data architectures in Angular & NGRX IntroductionData architectures in Angular & NGRX Introduction
Data architectures in Angular & NGRX Introduction
 
Let's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScriptLet's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScript
 

Similaire à Mobx for Dummies - Yauheni Nikanowich - React Warsaw #5

downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptxNavneetKumar111924
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
React and MobX: A Truly Reactive App
React and MobX:  A Truly Reactive AppReact and MobX:  A Truly Reactive App
React and MobX: A Truly Reactive AppJacob Orshalick
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesWalking Tree Technologies
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksSamundra khatri
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to ReduxAmin Ashtiani
 
Sagas Middleware Architecture
Sagas Middleware ArchitectureSagas Middleware Architecture
Sagas Middleware ArchitectureMateusz Bosek
 
FRONTEND BOOTCAMP 4.pptx
FRONTEND BOOTCAMP 4.pptxFRONTEND BOOTCAMP 4.pptx
FRONTEND BOOTCAMP 4.pptxEhtesham46
 
Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Flipkart
 
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
Advanced Redux architecture - WHAT/WHEN/WHY/HOWAdvanced Redux architecture - WHAT/WHEN/WHY/HOW
Advanced Redux architecture - WHAT/WHEN/WHY/HOWMateusz Bosek
 
Getting started with react & redux
Getting started with react & reduxGetting started with react & redux
Getting started with react & reduxGirish Talekar
 
Corso su ReactJS
Corso su ReactJSCorso su ReactJS
Corso su ReactJSLinkMe Srl
 
React gsg presentation with ryan jung & elias malik
React   gsg presentation with ryan jung & elias malikReact   gsg presentation with ryan jung & elias malik
React gsg presentation with ryan jung & elias malikLama K Banna
 

Similaire à Mobx for Dummies - Yauheni Nikanowich - React Warsaw #5 (20)

MobX for dummies
MobX for dummiesMobX for dummies
MobX for dummies
 
Mob x in react
Mob x in reactMob x in react
Mob x in react
 
Redux
ReduxRedux
Redux
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
 
Redux essentials
Redux essentialsRedux essentials
Redux essentials
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
React and MobX: A Truly Reactive App
React and MobX:  A Truly Reactive AppReact and MobX:  A Truly Reactive App
React and MobX: A Truly Reactive App
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to Redux
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
Sagas Middleware Architecture
Sagas Middleware ArchitectureSagas Middleware Architecture
Sagas Middleware Architecture
 
FRONTEND BOOTCAMP 4.pptx
FRONTEND BOOTCAMP 4.pptxFRONTEND BOOTCAMP 4.pptx
FRONTEND BOOTCAMP 4.pptx
 
Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"
 
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
Advanced Redux architecture - WHAT/WHEN/WHY/HOWAdvanced Redux architecture - WHAT/WHEN/WHY/HOW
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
 
Getting started with react & redux
Getting started with react & reduxGetting started with react & redux
Getting started with react & redux
 
React
ReactReact
React
 
Corso su ReactJS
Corso su ReactJSCorso su ReactJS
Corso su ReactJS
 
React gsg presentation with ryan jung & elias malik
React   gsg presentation with ryan jung & elias malikReact   gsg presentation with ryan jung & elias malik
React gsg presentation with ryan jung & elias malik
 

Dernier

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptrcbcrtm
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 

Dernier (20)

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.ppt
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 

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