js-react-store

Very small store library to be used in React - mainly to be used locally within components

Usage no npm install needed!

<script type="module">
  import jsReactStore from 'https://cdn.skypack.dev/js-react-store';
</script>

README

js-react-store

Very small store library to be used in React - mainly to be used locally within components

Licence npm version Build status Coverage status

Installation

npm install --save js-react-store

Usage

import React from 'react'
import ReactDOM from 'react-dom'
import { initStore, useStore } from 'js-react-store'

const { useCallback } = React

function createStore() { 
  const [self, update] = initStore({
    count: initialValue,

    increment() {
      increase(1)
    },

    decrement() {
      increase(-1)
    }
  })

  // private
  function increase(delta: number) {
    update(() => {
      self.count += delta
    })
  }
  
  return self
}

function Counter() {
  const
    store = useStore(createStore),
    increment = useCallback(() => store.increment(), []),
    decrement = useCallback(() => store.decrement(), [])

  return (
    <div>
      <label>Counter: </label>
      <button onClick={decrement}>-</button>
      {` ${store.count} `}
      <button onClick={increment}>+</button>
    </div>
  )
}

ReactDOM.render(<Counter/>, document.getElementById('main-content'))

License

"js-react-store" is licensed under LGPLv3.

Project status

"js-react-store" is currently in alpha status.