5.0.0 • Published 3 years ago

@bytesoftio/use-store v5.0.0

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

@bytesoftio/use-store

Installation

yarn add @bytesoftio/use-store or npm install @bytesoftio/use-store

Table of contents

Description

This package provides a React integration for @bytesoftio/store.

useStore

Use this helper to hook up a store inside a React component.

import React from "react"
import { createStore } from "@bytesoftio/store"
import { useStore } from "@bytesoftio/use-store"

const globalStore = createStore({count: 0})

const Component = () => {
  // create a new store from initial state, returns a state object and 
  // the actual store that has been connected to React
  const store1 = useStore(() => ({count: 0}))

  // create a new store through an initializer / factory function
  const store2 = useStore(globalStore) 
  
  const increment = () => store1.set({ count: state.count + 1 })
  const incrementGlobal = () => store2.set({ count: globalState.count + 1 })
 
  return (
    <div>
      <button onClick={increment}>local count: {store1.get().count}</button>    
      <button onClick={incrementGlobal}>global count: {globalStore.get().count}</button>    
    </div>
  )
} 
5.0.0

3 years ago

4.0.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago