Skip to content

lovetingyuan/react-atomic-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-atomic-store

Install

npm install react-atomic-store

Usage

import { createStore } from 'react-atomic-store'

const { useStore } = createStore({ foo: 1, bar: true })

function Bar() {
  const { bar, setBar } = useStore()
  return (
    <>
      <p>bar: {bar.toString()}</p>
      <button
        onClick={() => {
          setBar(v => !v)
        }}
      >
        change bar
      </button>
    </>
  )
}

function App() {
  const { foo, setFoo, bar } = useStore()
  return (
    <div>
      foo: {foo}
      <button
        onClick={() => {
          setFoo(foo + 1)
        }}
      >
        add foo
      </button>
      <Bar />
    </div>
  )
}

API

Only one api: createStore

const { useStore, getStoreMethods, getStoreState, getStateSnapshot, subscribeStore } = createStore({
  foo: 1,
  bar: false,
})

Return value of createStore:

  • useStore

    get current store state and get/set methods

    function MyComp() {
      const { foo, setFoo, getFoo, bar, setBar, getBar } = useStore()
      return (
        <div
          onClick={() => {
            setFoo()
          }}
        >
          {foo}
        </div>
      )
    }
  • getStoreMethods

    get current store get/set methods

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published