0.2.5 • Published 2 years ago

immex v0.2.5

Weekly downloads
21
License
MIT
Repository
github
Last release
2 years ago

Immex

  • based on immer.js
  • (global) statement management
  • react hooks friendly
  • insanely easy to use, just one API
  • support async/await reducers

Example

See examples here, or

Open in Gitpod

Installation

npm install --save immex immer

Usage

Define your store/reducer and export the react hook

// useCalculator.js
import immex from 'immex'

// immer reducer function
// refer: https://immerjs.github.io/immer/docs/curried-produce
const calculator = (draft, {operator, payload}) => {
  switch (operator) {
    case 'add':
      draft.value += payload
      break
    case 'sub':
      draft.value -= payload
      break
    default:
  }
}

const useCalculator = immex(
  calculator,
  {value: 0} // initial state
)
export default useCalculator

Use the exported react hook in function components, any change to the state will be updated in all components automatically

// foo.tsx
import React from 'react'
import useCalculator from './useCalculator'

export default () => {
  const [{value}, dispatch] = useCalculator()
  return (
    <>
      <div>
        <span>Foo:</span>
        {value}
      </div>
      <div>
        <button onClick={() => dispatch({ operator: "add", payload: 1 })}>+</button>
        <button onClick={() => dispatch({ operator: "sub", payload: 1})}>-</button>
      </div>
    </>
  )
}

Another component

// bar.tsx
import React from 'react'
import useCalculator from './useCalculator'

export default () => {
  const [{value}] = useCalculator()
  // this value keeps update to the one in component foo
  return (
    <div>
      <span>Bar:</span>
      {value}
    </div>
  )
}
0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.13

3 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago