@bytesoftio/use-store
TypeScript icon, indicating that this package has built-in type declarations

5.0.0 • Public • Published

@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>
  )
} 

Readme

Keywords

none

Package Sidebar

Install

npm i @bytesoftio/use-store

Weekly Downloads

0

Version

5.0.0

License

MIT

Unpacked Size

5.14 kB

Total Files

12

Last publish

Collaborators

  • maximkott