Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MobX - Performance and Sanity

MobX - Performance and Sanity

Slides for Mobx - Performance and Sanity talk as given in ReactNYC meetup group

Avatar for adam klein

adam klein

July 26, 2018
Tweet

More Decks by adam klein

Other Decks in Programming

Transcript

  1. • Persist state to a local storage and then boot

    up from it, out of the box. • Pre-fill state on the server, send it to the client in HTML, and boot up from it, out of the box. • Serialize user actions and attach them, together with a state snapshot, to automated bug reports, so that the product developers can replay them to reproduce the errors. • Pass action objects over the network to implement collaborative environments without dramatic changes to how the code is written. • Maintain an undo history or implement optimistic mutations without dramatic changes to how the code is written. • Travel between the state history in development, and re-evaluate the current state from the action history when the code changes, a la TDD. • Provide full inspection and control capabilities to the development tooling so that product developers can build custom tools for their apps. • Provide alternative UIs while reusing most of the business logic. Why Redux
  2. People often use MobX as alternative for Redux. Please note

    that MobX is just a library to solve a technical problem and not an architecture MICHEL WESTRATE M O B X C R E A T O R
  3. • Persist state to a local storage and then boot

    up from it, out of the box. • Pre-fill state on the server, send it to the client in HTML, and boot up from it, out of the box. • Serialize user actions and attach them, together with a state snapshot, to automated bug reports, so that the product developers can replay them to reproduce the errors. • Pass action objects over the network to implement collaborative environments without dramatic changes to how the code is written. • Maintain an undo history or implement optimistic mutations without dramatic changes to how the code is written. • Travel between the state history in development, and re-evaluate the current state from the action history when the code changes, a la TDD. • Provide full inspection and control capabilities to the development tooling so that product developers can build custom tools for their apps. • Provide alternative UIs while reusing most of the business logic. Why Redux
  4. • Persist state to a local storage and then boot

    up from it, out of the box. • Pre-fill state on the server, send it to the client in HTML, and boot up from it, out of the box. • Serialize user actions and attach them, together with a state snapshot, to automated bug reports, so that the product developers can replay them to reproduce the errors. • Pass action objects over the network to implement collaborative environments without dramatic changes to how the code is written. • Maintain an undo history or implement optimistic mutations without dramatic changes to how the code is written. • Travel between the state history in development, and re-evaluate the current state from the action history when the code changes, a la TDD. • Provide full inspection and control capabilities to the development tooling so that product developers can build custom tools for their apps. • Provide alternative UIs while reusing most of the business logic. MobX State Tree
  5. Some Facts 15K stars G I T H U B

    3 latest commit D AYS A G O 5.0.3 version on npm L AT E S T 28 on egghead.io F R E E V I D E O S
  6. <input type="email"/> import { autorun } from 'mobx'; autorun(() =>

    { input.value = model.email; }); model.setEmail(‘[email protected]’) model.setEmail(‘[email protected]’) MobX API
  7. import { observable } from 'mobx'; class Model { @observable

    email = ‘[email protected]’; setEmail(email) { this.email = email; } } Reactive Object
  8. autorun(() => { input.value = model.email; }); model.setEmail(‘[email protected]’) model.setEmail(‘[email protected]’) ...

    Our Code MobX // add dependency { name: ‘Autorun1’, dependencies: [ { name: ‘model.email’ } ] }
  9. import { observable } from 'mobx'; class Model { @observable

    email = ‘[email protected]’; @observable isValid = true; setEmail(email) { this.email = email; this.isValid = this.email.match(regexp); } } Duplicate State
  10. import { observable, computed } from 'mobx'; class Model {

    @observable email = ‘[email protected]’; @computed get isValid() { return this.email.match(regexp); } setEmail(email) { this.email = email; } } Computed Value
  11. import { observable } from 'mobx'; class Model { @observable

    data; fetchData() { serverApi.getData().then((data) => { this.data = data; }); } } Async Operations
  12. class Input extends React.Component { render() { return ( <form>

    <input value={ model.email }/> <button disabled={ model.isValid }>Go</button> </form> ); } } React
  13. import { observer } from 'mobx-react'; @observer class Input extends

    React.Component { render() { return ( <form> <input value={ model.email }/> <button disabled={ model.isValid }>Go</button> </form> ); } } React
  14. Shopping Cart Demo g i t H u b .

    c o m / 5 0 0 t e c h / m o b x - s h o p p i n g - c a r t Dynamic Observable Properties Provider <=> inject Normalized State Store Hierarchy Auto persist to localStorage
  15. P O W E R 35 Thank You @ _

    5 0 0 T e c h s l i d e s h a r e . n e t / 5 0 0 T e c h