All Projects → unirakun → k-mst-onaction

unirakun / k-mst-onaction

Licence: MIT license
Listen to mobx-state-tree actions and react to them!

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to k-mst-onaction

nytimes-bestseller
NY Times best sellers list explorer
Stars: ✭ 35 (+133.33%)
Mutual labels:  mobx-state-tree
graph-client
light zero dependency graphql-client, supporting cache and SSR
Stars: ✭ 27 (+80%)
Mutual labels:  nantes
mst-effect
💫 Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.
Stars: ✭ 19 (+26.67%)
Mutual labels:  mobx-state-tree
react-mobx-router
Create React App with React Router 4 and MobX + Internationalization
Stars: ✭ 90 (+500%)
Mutual labels:  mobx-state-tree
popcorn-nantes
🐘Code source du site Popcorn Nantes 🍿: la plateforme pour trouver un·e développeur·e freelance à Nantes sans intermédiaire et sans frais
Stars: ✭ 87 (+480%)
Mutual labels:  nantes
umi-plugin-mobx
😍 use mobx-state-tree gracefully in umijs.
Stars: ✭ 33 (+120%)
Mutual labels:  mobx-state-tree
hoc-react-animate
Add a CSS class whenever a props change (or/and at mount)
Stars: ✭ 14 (-6.67%)
Mutual labels:  nantes
mobx-state-tree-router
State-based router for React and MobX State Tree
Stars: ✭ 18 (+20%)
Mutual labels:  mobx-state-tree
mst-persist
Persist and hydrate MobX-state-tree stores (in < 100 LoC)
Stars: ✭ 75 (+400%)
Mutual labels:  mobx-state-tree
k-redux-factory
Factory of Redux reducers and their associated actions and selectors.
Stars: ✭ 18 (+20%)
Mutual labels:  nantes
k-ramel
State manager for your components apps, the safe and easy way
Stars: ✭ 20 (+33.33%)
Mutual labels:  nantes
use-bus
React hook to subscribe and dispatch events accros React components
Stars: ✭ 51 (+240%)
Mutual labels:  nantes
Prime
✨Open Source GraphQL CMS
Stars: ✭ 1,675 (+11066.67%)
Mutual labels:  mobx-state-tree
Mobx State Tree
Full-featured reactive state management without the boilerplate
Stars: ✭ 6,317 (+42013.33%)
Mutual labels:  mobx-state-tree
mst-react-ts-guide
Guide on mobx-state-tree with react in TS 😏
Stars: ✭ 61 (+306.67%)
Mutual labels:  mobx-state-tree
label-studio-frontend
Data labeling react app that is backend agnostic and can be embedded into your applications — distributed as an NPM package
Stars: ✭ 230 (+1433.33%)
Mutual labels:  mobx-state-tree

k-mst-onaction

🚧 🚧 RIGHT NOW THIS MIDDLEWARE DOESN'T WORK WITH AN UGLIFY VERSION OF YOUR MOBX-STATE-TREE STORE, LOOK A THIS MOBX_STATE_TREE ISSUE FOR MORE INFORMATIONS: Issue #492 🚧 🚧

Listen to mobx-state-tree actions and react to them !

Make your mobx-state-tree store a real tree, not a graph

CircleCI Coverage Status NPM Version Size

Contents

Purpose

The main purpose is to get rid of store interdependencies and to be more into a reactive way of coding.

Why

you can see this issue.

What we want is to pass from an actions dependencies graph to a tree:

  

Installation

  • yarn add k-mst-onaction
  • npm i k-mst-onaction

API

First try

  1. Import the middleware from k-mst-onaction: import onAction from 'k-mst-onaction'
  2. Write your reaction, the easiest way is to write it as a function:
const dispatch = (action, tree) => {
  const { fullpath, ended } = action

  if (fullpath === '/auth/login' && ended) {
    tree.ui.router.goToList()
  }
}
  1. Connect the middleware to your root store with addMiddleware from mobx-state-tree: addMiddleware(yourStore, onAction(dispatch))
  2. Voila !

Middleware API

As you see on the First try what you have to do is to give a dispatch function to the onAction middleware.

The dispatch function can be of two different types:

  • an array, in this case, each function of the array will be called
  • a function, in this case the function will be called
    • if the dispatch function returns an array, then the middleware will iterate over the array and call each functions that compose it

You can use the take helper to avoid dealing with the API and have a cleaner code.

From First try example code with take helper:

import { addMiddleware } from 'mobx-state-tree'
import onAction from 'k-mst-onaction'
import Store from './your-store-model'

// instanciate the store
const store = Store.create({})

// the actions to trigger
const dispatch = (action, tree) => [
  take.ended('/auth/login', () => { tree.ui.router.goToList() })
]

// attach the onAction middleware from k-mst-onaction
addMiddleware(store, onAction(dispatch))

Note that:

  • dispatch returns an array
  • we call take.ended which will test that the asynchronous action is ended
  • we pass the full action name (path + name) as first parameter
  • we pass the reaction as second one parameter

Take API

take is an helper that takes two arguments (take(test, reaction)):

  • first argument is the test, it can be
    • a string: this string will be converted to a regular expression then the match is tested with fullpath
      • '/user/add' will work against '/user/add/'
      • '/user/:id/setName' will work against '/user/12/setName'
    • a regular expression: then the fullpath is tested over the regular expression
    • a function: the function is called and should return true to have the reaction called
      • the function takes two arguments: the action to test and the current tree (your store instance)
  • second argument is the reaction, this is a function with two parameters (reaction(action, tree)):
    • action is the action that pass the test (first argument of take)
    • tree is your current store instance, so you can call action on it !

Action API

As you can see, the action object is given to your dispatch function, and to first and second parameters of take helper. This action owns these fields:

  • path: the action path from the root store
  • name: the action name
  • fullpath: path + '/' + name
  • ended: for asynchronous action only, it means the aynchronous action is ended

Examples

We will write 4 ways of doing a router redirection after our login is successful:

  • dispatch is a function (that doesn't return an array)
  • dispatch is a function that returns an array
    • with a not pure take helper function use
    • with a pure take helper function use
  • dispatch is an array

dispatch is a function (that doesn't return an array)

import { addMiddleware } from 'mobx-state-tree'
import onAction from 'k-mst-onaction'
import Store from './your-store-model'

const store = Store.create({})

const dispatch = (action, tree) => {
  const { fullpath, ended } = action

  if (ended && fullpath === '/auth/login') {
    tree.ui.router.goToList()
  }
}

addMiddleware(store, onAction(dispatch))

dispatch is a function that returns an array - impure take

import { addMiddleware } from 'mobx-state-tree'
import onAction, { take } from 'k-mst-onaction'
import Store from './your-store-model'

const store = Store.create({})

const dispatch = (action, tree) => [
  take.ended('/auth/login', () => { tree.ui.router.goToList() }),
]

addMiddleware(store, onAction(dispatch))

dispatch is a function that returns an array - pure take

import { addMiddleware } from 'mobx-state-tree'
import onAction, { take } from 'k-mst-onaction'
import Store from './your-store-model'

const store = Store.create({})

const dispatch = () => [
  take.ended('/auth/login', (action, tree) => { tree.ui.router.goToList() }),
]

addMiddleware(store, onAction(dispatch))

dispatch is an array ️❤️

❤️ This is the recommended way of using this middleware ❤️

import { addMiddleware } from 'mobx-state-tree'
import onAction, { take } from 'k-mst-onaction'
import Store from './your-store-model'

const store = Store.create({})

const dispatch = [
  take.ended('/auth/login', (action, tree) => { tree.ui.router.goToList() }),
]

addMiddleware(store, onAction(dispatch))

About uni rakun

uni rakun is created by two passionate french developers.

Do you want to contact them ? Go to their website

Guillaume CRESPEL Fabien JUIF
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].