All Projects → bcherny → Undux

bcherny / Undux

Licence: mit
⚡️ Dead simple state for React. Now with Hooks support.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects
HTML
75241 projects

Projects that are alternatives of or similar to Undux

Suas Android
Unidirectional data flow architecture implementation for Android
Stars: ✭ 80 (-94.62%)
Mutual labels:  flux
Spring 5 Examples
This repository is contains spring-boot 2 / spring framework 5 project examples. Using reactive programming model / paradigm and Kotlin
Stars: ✭ 87 (-94.15%)
Mutual labels:  flux
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (-92.81%)
Mutual labels:  flux
Fluxus
Flux for SwiftUI, inspired by Vuex
Stars: ✭ 81 (-94.56%)
Mutual labels:  flux
Rx Connect
Glue your state and pure React components with RxJS
Stars: ✭ 86 (-94.22%)
Mutual labels:  flux
Qiitawithfluxsample
A sample project uses Flux and MVVM features with RxSwift.
Stars: ✭ 94 (-93.68%)
Mutual labels:  flux
Material Flux
No magic flux implementation library.
Stars: ✭ 76 (-94.89%)
Mutual labels:  flux
Tiny Atom
Pragmatic and concise state management.
Stars: ✭ 109 (-92.67%)
Mutual labels:  flux
K8s Plus Aws Gitops
An approach for GitOps of AWS backing resources like databases with CodePipeline together with Kubernetes via Flux
Stars: ✭ 87 (-94.15%)
Mutual labels:  flux
Prex
🔁Unidirectional data flow architecture with MVP and Flux combination for Swift
Stars: ✭ 102 (-93.15%)
Mutual labels:  flux
Speedux
Speedux is an opinionated library that makes Redux much more fun to use.
Stars: ✭ 82 (-94.49%)
Mutual labels:  flux
Freezer
A tree data structure that emits events on updates, even if the modification is triggered by one of the leaves, making it easier to think in a reactive way.
Stars: ✭ 1,268 (-14.78%)
Mutual labels:  flux
Hover
A very lightweight data store with action reducers and state change listeners.
Stars: ✭ 97 (-93.48%)
Mutual labels:  flux
Fluxxan
Fluxxan is an Android implementation of the Flux Architecture that combines concepts from both Fluxxor and Redux.
Stars: ✭ 80 (-94.62%)
Mutual labels:  flux
Reactor Netty
TCP/HTTP/UDP/QUIC client/server with Reactor over Netty
Stars: ✭ 1,743 (+17.14%)
Mutual labels:  flux
Purescript Graphql
End to End typesafe GraphQL with PureScript
Stars: ✭ 79 (-94.69%)
Mutual labels:  typesafe
React Waterfall
React store built on top of the new context API
Stars: ✭ 1,318 (-11.42%)
Mutual labels:  flux
Go Sct
A color temperature setting library and CLI that operates in a similar way to f.lux and Redshift.
Stars: ✭ 112 (-92.47%)
Mutual labels:  flux
Nucleo
🏋️‍♀️ Nucleo is a strongly typed and predictable state container library for JavaScript ecosystem written in TypeScript
Stars: ✭ 109 (-92.67%)
Mutual labels:  flux
Juicr.js
A simple (and tiny <1kb) redux inspired reducer for handling state changes.
Stars: ✭ 102 (-93.15%)
Mutual labels:  flux

undux

Build Status npm mit ts flow

Dead simple state management for React


📖 Official docs: https://undux.org


Install (with RxJS v5 or v6 - recommended)

# Using Yarn:
yarn add undux

# Or, using NPM:
npm install undux --save

Install (with RxJS v4)

# Using Yarn:
yarn add undux@^3

# Or, using NPM:
npm install undux@^3 --save

Design Goals

  1. Complete type-safety, no exceptions
  2. Super easy to use: forget actions, reducers, dispatchers, containers, etc.
  3. Familiar abstractions: just get and set

Read more here

Use

1. Create a store

import { createConnectedStore } from 'undux'

// Create a store with an initial value.
export default createConnectedStore({
  one: 0,
  two: 0
})

Be sure to define a key for each value in your model, even if the value is initially undefined.

2. Connect your React components

With React Hooks:

import Store from './MyStore'

// Re-render the component when the store updates.
function MyComponent() {
  let store = Store.useStore()
  return <div>
    <NumberInput onChange={store.set('one')} value={store.get('one')} />
    <NumberInput onChange={store.set('two')} value={store.get('two')} />
    Sum: {store.get('one') + store.get('two')}
  </div>
}

function NumberInput() {
  return <input
    onChange={e => this.props.onChange(parseInt(e.target.value, 10))}
    type="number"
    value={this.props.value}
  />
}

export default MyComponent

Without React Hooks:

import Store from './MyStore'

// Re-render the component when the store updates.
class MyComponent extends React.Component {
  render() {
    let store = this.props.store
    return <div>
      <NumberInput onChange={store.set('one')} value={store.get('one')} />
      <NumberInput onChange={store.set('two')} value={store.get('two')} />
      Sum: {store.get('one') + store.get('two')}
    </div>
  }
}

class NumberInput extends React.Component {
  render() {
    return <input
      onChange={e => this.props.onChange(parseInt(e.target.value, 10))}
      type="number"
      value={this.props.value}
    />
  }
}

export default Store.withStore(MyComponent)

3. Put your app in an Undux Container

import MyComponent from './MyComponent'
import Store from './MyStore'

class MyApp extends React.Component {
  render() {
    return <Store.Container>
      <MyComponent />
    </Store.Container>
  }
}

export default MyApp

That's all there is to it.

Open this code in playground.

Features

Effects

Though Undux automatically re-renders your connected React components for you when the store updates, it also lets you subscribe to changes to specific fields on your store. Undux subscriptions are full Rx observables, so you have fine control over how you react to a change:

import { debounce, filter } from 'rxjs/operators'

store
  .on('today')
  .pipe(
    filter(date => date.getTime() % 2 === 0), // Only even timestamps.
    debounce(100) // Fire at most once every 100ms.
  )
  .subscribe(date =>
    console.log('Date changed to', date)
  )

You can even use Effects to trigger a change in response to an update:

store
  .on('today')
  .pipe(
    debounce(100)
  )
  .subscribe(async date => {
    let users = await api.get({ since: date })
    store.set('users')(users)
  })

In order to keep its footprint small, Undux does not come with RxJS out of the box. However, Undux does come with a minimal implementation of parts of RxJS, which interoperates with RxJS operators. To use RxJS operators, you'll need to install RxJS first:

npm install rxjs --save

Partial application

Partially apply the set function to yield a convenient setter:

let setUsers = store.set('users')
setUsers(['amy'])
setUsers(['amy', 'bob'])

Built-in logger

Undux works out of the box with the Redux Devtools browser extension (download: Chrome, Firefox, React Native). To enable it, just wrap your store with the Redux Devtools plugin:

import { createConnectedStore, withReduxDevtools } from 'undux'

let store = createConnectedStore(initialState, withReduxDevtools)

Redux Devtools has an inspector, a time travel debugger, and jump-to-state built in. All of these features are enabled for Undux as well. It looks like this:

Alternatively, Undux has a simple, console-based debugger built in. Just create your store with withLogger higher order store, and all model updates (which key was updated, previous value, and new value) will be logged to the console.

To enable the logger, simply import withLogger and wrap your store with it:

import { createConnectedStore, withLogger } from 'undux'

let store = createConnectedStore(initialState, withLogger)

The logger will produce logs that look like this:

Effects

Undux is easy to modify with effects. Just define a function that takes a store as an argument, adding listeners along the way. For generic plugins that work across different stores, use the .onAll method to listen on all changes on a store:

// MyStore.ts (if using TypeScript)
import { Effects } from 'undux'

type State = {
  // ...
}

export type StoreEffects = Effects<State>

// MyEffects.ts
import { StoreEffects } from './MyStore'

let withLocalStorage: StoreEffects = store => {

  // Listen on all changes to the store.
  store.onAll().subscribe(({ key, value, previousValue }) =>
    console.log(key, 'changed from', previousValue, 'to', value)
  )

}

Recipes

Creating a store (TypeScript)

import { createConnectedStore, Effects, Store } from 'undux'

type State = {
  foo: number
  bar: string[]
}

let initialState: State = {
  foo: 12,
  bar: []
}

export default createConnectedStore(initialState)

export type StoreProps = {
  store: Store<State>
}

export type StoreEffects = Effects<State>

See full example (in JavaScript, TypeScript, or Flow) here.

Stateless component with props (TypeScript)

Have your own props? No problem.

import MyStore, { StoreProps } from './MyStore'

type Props = StoreProps & {
  foo: number
}

function MyComponent(props: Props) {
  return <>
    Today is {props.store.get('today')}
    Foo is {props.foo}
  </>
}

export default MyStore.withStore(MyComponent)

// Usage
<MyComponent foo={3} />

See full example (in JavaScript, TypeScript, or Flow) here.

Stateful component with props (TypeScript)

Undux is as easy to use with stateful components as with stateless ones.

import MyStore, { StoreProps } from './MyStore'

type Props = StoreProps & {
  foo: number
}

class MyComponent extends React.Component<Props> {
  render() {
    return <>
      Today is {this.props.store.get('today')}
      Foo is {this.props.foo}
    </>
  }
}

export default MyStore.withStore(MyComponent)

// Usage
<MyComponent foo={3} />

See full example (in JavaScript, TypeScript, or Flow) here.

Undux + Hot module reloading

See a full example here.

Undux + TodoMVC

See the Undux TodoMVC example here.

Design philosophy

Goal #1 is total type-safety.

Getting, setting, reading, and listening on model updates is 100% type-safe: use a key that isn't defined in your model or set a key to the wrong type, and you'll get a compile-time error. And connected components and Effects are just as type-safe.

Goal #2 is letting you write as little boilerplate as possible.

Define your model in a single place, and use it anywhere safely. No need to define tedious boilerplate for each field on your model. Container components and action creators are optional - most of the time you don't need them, and can introduce them only where needed as your application grows.

Goal #3 is familiar abstractions.

No need to learn about Actions, Reducers, or any of that. Just call get and set, and everything works just as you expect.

Tests

yarn test

License

MIT

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].