Skip to content

Utility function to create a mobx store without the use of es6 classes.

Notifications You must be signed in to change notification settings

fvnilo/mobx-noclass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm version

mobx-noclass

Utility function to create a mobx store without the use of es6 classes.

Why?

I tend to stay away from ES6 classes. They do are convenient, but I have some issues with them:

  • They favor classical inheritance over prototypal inheritance.
  • They favor inheritance over composition.
  • They re-introduce the new keyword which comes from languages that works with classical inheritance and I feel that it strays away from JavaScript's true nature to make developers coming from Java, C++ and other similar languages more comfortable.
  • Creating private fields is either hacky or not elegant.

For more information, consult this awesome list.

Usage

createMobxStore(observables, actions)

Creates a new mobx store by passing the observables and the actions on the observables as objects.

Having the actions defined in objects will allow you to have closures that will serve as private fields.

Example

import { createMobxStore } from 'mobx-noclass';

let store = createMobxStore({
  value: 0,
  next() {
    return this.value + 1;
  },
  previous() {
    return this.value - 1;
  }
},
{
  inc() {
    return this.value++;
  },
  dec() {
    return this.value--;
  }
});

About

Utility function to create a mobx store without the use of es6 classes.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published