6.0.5 • Published 3 years ago

eztore v6.0.5

Weekly downloads
203
License
MIT
Repository
github
Last release
3 years ago

Installation

npm i eztore

Usage

import { getStore } from "eztore"

export const useStore = getStore({
  [key1]: {
      state: initialState,
      reducers: {
        [reducer]: (state, payload) => newState
      }
  },
  [key2]: {
      state: initialState,
      reducers: {
        [reducer]: (state, payload) => newState
      }
  },
  ...
}

The getStore function returns a hook function like function useStore(key, subscribe = true) { ... }. Each call to getStore will return a new store.

You can access the state in any component by calling useStore(key[, subscribe]), which returns an array [state, dispatch] like useReducer. If you call useStore with subscribe = false it will only return the dispatch function and will not rerender on state changes.

Example

store.js

import { getStore } from "eztore"

export const useStore = getStore({
  name: {
    state: "",
    reducers: {
      default(state, payload) {
        return payload
      },
      upperCase(state, payload) {
        return payload.toUpperCase()
      }
    }
  },
})

Component.js

import React from "react"
import { useStore } from "store"

function Component() {
  const [name, dispatchName] = useStore("name")

  function onChange(event) {
    dispatchName({ payload: event.target.value })
    // if we want uppercase
    // dispatchName({ type: "upperCase", payload: event.target.value })
  }

  return <input value={name} onChange={onChange} />
}

export default Component
6.0.5

3 years ago

6.0.4

3 years ago

6.0.3

3 years ago

6.0.2

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.1.0

3 years ago

3.0.4

4 years ago

3.0.3

4 years ago

4.0.0

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago