Skip to content

simplux - simple, scalable state management for web applications

License

Notifications You must be signed in to change notification settings

MrWolfZ/simplux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simplux - simple, scalable state management for web applications

simplux is state management as it should be: simple to use, no boilerplate, type-safe but not verbose, and with excellent testability. simplux provides out-of-the-box support for React and Angular, but can be used with any framework.

npm version Build Status codecov license

Quickstart

npm i @simplux/core -S
import { createSimpluxModule, createMutations, createSelectors } from '@simplux/core'

// state in simplux is contained in modules identified by a unique name
const counterModule = createSimpluxModule('counter', { value: 0 })

export const counter = {
  ...counterModule,

  // use mutations to modify the state
  ...createMutations(counterModule, {
    increment(state) {
      state.value += 1
    },
    incrementBy(state, amount: number) {
      state.value += amount
    },
  }),

  // use selectors to access the state
  ...createSelectors(counterModule, {
    value: (state) => state.value,
    plus: (state, amount: number) => state.value + amount,
  }),
}

counter.increment()
console.log('incremented counter:', counter.value())
console.log('counter value + 2:', counter.plus(2))

counter.incrementBy(5)
console.log('incremented counter by 5:', counter.value())

See this example in action here. For a more detailed look into how simplux can make your life simple follow our recipe for getting started.

React

npm i @simplux/core @simplux/react -S
import { SimpluxProvider, useSimplux } from '@simplux/react'
import React from 'react'
import { render } from 'react-dom'
import { counter } from './counter.module'

const Counter = () => {
  const value = useSimplux(counter.value)
  const valuePlusFive = useSimplux(counter.plus, 5)

  return (
    <>
      <span>value: {value}</span>
      <br />
      <span>value + 5: {valuePlusFive}</span>
      <br />
      <button onClick={counter.increment}>Increment</button>
      <br />
      <button onClick={() => counter.incrementBy(5)}>Increment by 5</button>
    </>
  )
}

const App = () => (
  <SimpluxProvider>
    <Counter />
  </SimpluxProvider>
)

render(<App />, document.getElementById('root'))

See this example in action here. For a more detailed look into how simplux can power up your React application follow our recipe for using simplux in your React application.

Angular

See the recipe for using simplux in your Angular application.

Recipes

Instead of traditional documentation simplux has these recipes that will show you how it can make life simple for you. Each recipe will help you solve one particular task that you will typically face during development.

For every "How do I do X?" you have ever asked yourself, there should be a recipe here. If you find one that is missing, please let us know by creating an issue or even better, provide the recipe as a pull request.

Basics

Advanced

React

Angular

Motivation

When discovering this library your first thought may have been: "Are you kidding me, yet another state management library?" That sentiment is perfectly understandable. There are many existing options for managing your state in web applications. If you are already using one of those and it works for you, then you should probably stick with it. However, simplux brings some unique points to the table that make it a worthwhile addition to the state management ecosystem:

  • excellent task-driven documentation: a lot of effort went into writing our recipes. While most other libraries have documentation that is centered around explaining what they do, our task-driven documentation is focused on showing you how simplux helps you to solve your concrete tasks. We also provide code sandboxes for every recipe that allow you to interact with the code while reading the recipe, which greatly improves the learning experience.

  • strong focus on testability: testing is a very important topic that is sadly often neglected. simplux takes testability very seriously and makes sure that you know how you can test the code you have written using it (you may have noticed that the recipe immediately following getting started in the list above already shows you how you can test the code from the first recipe).

  • optimized for TypeScript: simplux is built with and for TypeScript. Sometimes TypeScript code can be a bit verbose. We put a lot of effort into ensuring that the amount of type annotations in your code is minimized by leveraging type inference wherever possible. That said simplux can also be used with plain JavaScript, in which case your IDE may still use the TypeScript information to help you due to our bundled typings.

  • out-of-the-box solutions for many common yet complex use-cases: Have you ever tried setting up hot module reloading or code splitting with React and Redux? It can be quite tricky. simplux aims to solve as many of these complex use-cases by providing zero-configuration out-of-the-box solutions.

  • modular and extensible architecture: Our core package only contains the bare minimum that is required to use simplux. All other advanced functionality is added via extension packages. On one hand this allows you to pick and choose what functionality you want to use without paying for anything that you don't. On the other hand it allows adding new extension packages without risk of breaking any existing functionality.

Prior Art

This library was heavily inspired by Rematch and shares a lot of ideas with it.

Open points

core

  • add a function that "activates" subscriptions which has to be called during app initialization
  • add a resetSimplux function that resets all modules to their initial state (useful for SSR)
  • add a combineSelectors utility function
  • add a subscribe function to selectors to get notified of value changes

testing

  • add functions to throw when calling unmocked mutations or effects

router

  • add type tests
  • capture navigation history
  • allow going back to previously active route

browser-router

  • add support for base href in history mode

  • intercept click events on window and navigateToUrl for same tab target on same origin (only if within base-href)

  • add navigation option for replacing browser history entry

  • allow ?[, [?, &[, and [& for optional query parameters

  • add navigation option for setting URL at start or end of navigation (or not at all)

  • support object parameters

  • add type tests

  • add support for setting hash parameter in history mode

  • add support for hash mode

  • allow defining default values for parameters (e.g. /:id:string|defaultValue/)

  • alternatively add function configuration parameter to fill in defaults dynamically (also infer correct full parameter type from return value of callback)

entities

  • create package for managing collections of entities
  • create default set of entity management mutations
  • allow creating custom mutations that act on one entity
  • create default set of selectors for entities

react

nothing

angular

nothing

misc

  • docs: create website
  • recipes: add example effects to recipe for creating non-trivial modules and explicitly mention mixin pattern
  • build: create root jest config to run tests of all projects at once
  • build: switch to eslint

Contributing

If you want to help with the development of this library please have a look at the contributing guidelines.

License

Everything in this repository is licensed under the MIT License unless otherwise specified.

Copyright (c) 2019-present Jonathan Ziller

About

simplux - simple, scalable state management for web applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published