0.3.3 • Published 8 months ago

strictly-formed v0.3.3

Weekly downloads
3
License
ISC
Repository
github
Last release
8 months ago

Strictly Formed

Component state bindings for redux and typescript

Install

npm install strictly-formed

API

Redux Store

  • strictly-formed state expects to be mapped under the reducer key components.
  import { combineReducers, createStore } from 'redux'
  import { componentStateReducer, ComponentState, ComponentStateAction } from 'strictly-formed'

  export type Action = ComponentStateAction // ...
  export type State = {
    // ...
    components: ComponentState
  }

  export const store = createStore(
    combineReducers({
      // ...
      components: componentStateReducer,
    }),
    // ...
  )

useComponentState

  • an unopinionated state hook bound to redux.
  • the interface is modeled after React.useState
  • the createId utility will generate an ID that is bound to the component type. This will enable you to access the component state from redux without needing type assertions.
export const useComponentState: <Component>(id: Id<Component>, initial: Component) => [
  Component,
  (value?: Component | ((state: Component) => Component)) => void,
  boolean
]
  • example:
import { useComponentState, createId } from 'strictly-formed'

type State = {
  name: string
  age?: number
}

const COMPONENT_ID = createId<State>('COMPONENT_ID')

const Component = (props) => {
  const [state, set, exists] = useComponentState(COMPONENT_ID, { name: '' })
  // ...
}

Action Creators

setComponent

export const setComponent: <Component>(id: Id<Component>, value: Component) => {
    type: 'SET_COMPONENT';
    id: Id<Component>;
    value: Component;
};

clearComponent

export const clearComponent: <Component>(id: Id<Component>) => {
    type: 'CLEAR_COMPONENT';
    id: Id<Component>;
};

Selectors

getComponentState requires an "initial" argument, this will be returned if the component's state has not yet been initialized in redux. The returned value will always be typeof Component

export const getComponentState: <Component>(state: State, id: Id<Component>, initial: Component) => Component

getComponent will return undefined if the state has not been initialized in redux

export const getComponent: <Component>(state: State, id: Id<Component>) => Component | undefined

componentExists will return boolean value determining whether the component's state exists within redux

export const componentExists: <Component>(state: State, id: Id<Component>) => boolean

License

ISC

0.3.3

8 months ago

0.3.3-rc.1

1 year ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.3.0-rc.5

2 years ago

0.3.0-rc.6

2 years ago

0.3.0-rc.3

2 years ago

0.3.0-rc.4

2 years ago

0.3.0-rc.0

2 years ago

0.3.0-rc.1

2 years ago

0.3.0-rc.2

2 years ago

0.2.4

5 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago