js-react-store
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i js-react-store

Weekly Downloads

0

Version

0.0.8

License

LGPL-3.0

Unpacked Size

1.31 MB

Total Files

32

Last publish

Collaborators

  • mcjazzyfunky