0.0.8 • Published 5 years ago

js-react-store v0.0.8

Weekly downloads
1
License
LGPL-3.0
Repository
github
Last release
5 years ago

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.

0.0.8

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago