Skip to content

Instantly share code, notes, and snippets.

@StevePotter
Last active December 15, 2018 18:21
Show Gist options
  • Save StevePotter/9e4cdf0d7cb2a6a3929b37c8642adfd0 to your computer and use it in GitHub Desktop.
Save StevePotter/9e4cdf0d7cb2a6a3929b37c8642adfd0 to your computer and use it in GitHub Desktop.
App State Container Improvements

Improvements over Redux

  • Less boilerplate (no actions, for example)

Features

  • Declare a state container using a HOC
  • Easily add 'reducers', which become methods to the state object
  • Built-in update and reset functions
  • Add auto-calculated fields (like 'canNavigateToCompositionSection')
  • Easy persistence (sessionState)
  • Per-route persistance

Usage:

screen's index.js's compose

withStateContainer(
  default: { // or, (props) =>
     name: null,
     panel: 'metadata',
     compositionRights: {
       
     }
  },
  reducers: {
    changePanel: (newPanel) => ({
       panel: newPanel
    })
  },
  calculated: {
     isValid: (state) => !!state.name
  }
)

Then in template.js

Layers:

  • state values
  • computed values
  • reducers
  • utility functions (don't change state)

Ramblings:

  • should it be possible to add reducers further down in the component tree?
  • use context to pass down value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment