Giter Site home page Giter Site logo

hyfer-state's Introduction

Hyfer State Management - Observer vs Mobx

Introduction

The Observable pattern* for managing global application state was chosen in the class 11 project for educational purposes. However, now that the complexity of the application is increasing, all the extra code required for this solution is becoming a burden. Therefore, one of the objectives of the class 13 project is to simplify state management by migrating to mobx.

*Wikepedia: In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design.

Sample application

This sample application compares the two state management solutions currently implemented in Hyfer:

  1. Custom stores based on the Observer Pattern.
  2. Mobx stores.

The application consists of these three simple components:

Component Description
App The top level component.
ObsUser Uses an observable store.
MobxUser Uses a mobx store.

Both ObsUser and MobxUser components simply render an h2 element with the text Hello, name, where the value of name is obtained from a store.

In the componentDidMount() method of the App component the name values of the stores are dynamically set to Jim and Valerio respectively.

When you run the application you should see the following output:

Hello, Jim!
Hello, Valerio!

The store based on the Observable pattern can be found in obs-stores/UserStore.js. This is how most of the stores are currently implemented in Hyfer (however some of the stores do not use ES6 classes). The mobx store can be found in the mobx-stores folder.

If you compare the two store types and the two components where these stores are used you can see that a lot of boilerplate code is needed to support the stores based on the Observable pattern. Basically, these stores are used to copy global state from the store to local state in the component whenever a change is made to the global state. Components that use the Observable store must explicitly subscribe and unsubscribe through the componentDidMount() and componentWillUnmount() life cycle methods.

With the mobx solution there is no need to subscribe and unsubscribe, nor is there a need to maintain local component state. The store implementation is simpler too. There is much less code to write and worry about, which is a good thing!

hyfer-state's People

Contributors

remarcmij avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.