SkillAgentSearch skills...

Statex

StateX is a state management library for modern web applications with unidirectional data flow and immutable uni-state (just like redux)

Install / Use

/learn @rintoj/Statex
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

StateX

StateX is a state management library for modern web applications with unidirectional data flow and immutable uni-state. StateX is a predictable state container just like REDUX. It helps you implement a unidirectional data flow (Flux architecture) in an easy and elegant way without much boilerplate code. The main objective is to provide an implementation that has minimal touch points, while providing all the benefits of Redux. StateX uses rxjs library at its heart, hence promises efficient data flow. StateX is inspired by refluxjs and redux.

To enable seamless integration, StateX has specific APIs for Angular (2 or above) and React.

Note: StateX was originally written for angular - angular-reflux and later modified for react - react-reflux. Both of these packages are now migrated to StateX

Architecture

Flux is an architecture for unidirectional data flow. By forcing the data to flow in a single direction, Flux makes it easy to reason how data-changes will affect the application depending on what actions have been issued. The components themselves may only update application-wide data by executing an action to avoid double maintenance nightmares.

Flow

  • STATE - contains application wide data. Technically this is a single immutable JavaScript object containing every data that an application needs.

  • STORES - contain business logic - how an action should transform the application wide data represented by STATE

  • VIEWS - Views must react to the change in STATE. So an event is triggered when STATE changes, which VIEWS can consume to update itself with the new data.

  • ACTIONS - are dispatched whenever a view needs to change application state. The actions contain payload to help store complete the updates.

Install

npm install statex --save

Usage

StateX works with any modern JavaScript framework, however there are minor differences to how it is implemented for each framework.

This guide includes instructions to integrate StateX with the following combinations of frameworks and features

| Framework | Language | Decorator | | |-----------|------------------|-----------|---------------| | Angular | TypeScript | YES | (Recommended) | | Angular | TypeScript | NO | | | React | TypeScript | YES | (Recommended) | | React | ES6 | YES | | | React | ES6 | NO | |

Use @angular/cli to get started with Angular

Use react-ts to get started with React & TypeScript

Use create-react-app to get started with React & ES6

or use one of the following examples

Examples

5 Simple Steps

1. Define State

To get the best out of TypeScript, declare an interface that defines the structure of the application-state. This is optional if you don't want to use TypeScript.

export interface Todo {
  id?: string
  title?: string
  completed?: boolean
}

export type Filter = 'ALL' | 'ACTIVE' | 'COMPLETED'

export interface AppState {
  todos?: Todo[]
  filter?: Filter
}

2. Define Action

Define actions as classes with the necessary arguments passed on to the constructor. This way we will benefit from the type checking; never again we will miss-spell an action, miss a required parameter or pass a wrong parameter. Remember to extend the action from Action class. This makes your action listenable and dispatch-able.

Using TypeScript

import { Action } from 'statex';

export class AddTodoAction extends Action {
  constructor(public todo: Todo) {
    super()
  }
}

Using ES6

import { Action } from 'statex';

export class AddTodoAction extends Action {
  constructor(todo) {
    super()
    this.todo = todo
  }
}

3. Create Store & Bind Action

Stores are the central part of a Flux architecture. While most of the logics for a store are same, some of the minor details vary from framework to framework.

Angular with Decorator (Recommended)

Use @action decorator to bind a reducer function with an Action. The second parameter to the reducer function (addTodo) is an action (of type AddTodoAction); @action uses this information to bind the correct action. Also remember to extend this class from Store.

import { Injectable } from '@angular/core'
import { action, Store } from 'statex/angular'

@Injectable()
export class TodoStore extends Store {

  @action()
  addTodo(state: AppState, payload: AddTodoAction): AppState {
    return { todos: state.todos.concat([payload.todo]) }
  }
}

Angular without Decorator

import { Injectable } from '@angular/core'

@Injectable()
export class TodoStore {

  constructor() {
    new AddTodoAction(undefined).subscribe(this.addTodo, this)
  }

  addTodo(state: AppState, payload: AddTodoAction): AppState {
    return { todos: state.todos.concat([payload.todo]) }
  }
}

This store will be instantiated by Angular's dependency injection.

React - TypeScript with Decorators (Recommended)

Use @action decorator to bind a reducer function with an Action. The second parameter to the reducer function (addTodo) is an action (of type AddTodoAction); @action uses this information to bind the correct action.

import { AppState } from '../state';
import { AddTodoAction } from '../action';
import { action, store } from 'statex/react';

@store()
export class TodoStore {

  @action()
  addTodo(state: AppState, payload: AddTodoAction): AppState {
    return { todos: state.todos.concat([payload.todo]) }
  }
}

Stores must bind each action with the reducer function at the startup and also must have a singleton instance. Both of these are taken care by @store decorator.

React - ES6 with Decorators

import { AddTodoAction } from '../action';
import { action, store } from 'statex/react';

@store()
export class TodoStore {

  @action(AddTodoAction)
  addTodo(state, payload) {
    return { todos: state.todos.concat([payload.todo]) }
  }
}

@action takes an optional parameter - the action class. Always remember to add @store() to the class.

React - ES6 without Decorators

import { AddTodoAction } from '../action';

export class TodoStore {

  constructor() {
    new AddTodoAction().subscribe(this.addTodo, this)
  }

  addTodo(state, payload) {
    return { todos: state.todos.concat([payload.todo]) }
  }
}

new TodoStore()

Remember to instantiate the store at the end.

4. Dispatch Action

No singleton dispatcher! Instead StateX lets every action act as dispatcher by itself. One less dependency to define, inject and maintain.

new AddTodoAction({ id: 'sd2wde', title: 'Sample task' }).dispatch();

5. Consume Data

Use @data decorator and a selector function (parameter to the decorator) to get updates from application state. The property gets updated only when the value returned by the selector function changes from previous state to the current state. Additionally, just like a map function, you could map the data to another value as you choose.

We may, at times nee

View on GitHub
GitHub Stars68
CategoryDevelopment
Updated12mo ago
Forks18

Languages

TypeScript

Security Score

87/100

Audited on Apr 6, 2025

No findings