1.0.2 • Published 3 months ago

react-cafe v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago
npm i react-cafe

Create a store

// src/store/index.ts
import { createStore } from "react-cafe";

type MyStore = {
  count: number
}

export const myStore = createStore<MyStore>({
  count: 0,
  name: 'John'
})

Use in any React Component

// src/components/Counter.tsx
import React from 'react'
import { myStore } from '../store'

const Counter = () => {

  const [count, setCount] = myStore.useCount()

  const increment = ()=>{
    setCount(p => p + 1)
  }

  return (
    <div>
      <button onClick={increment} >Increment</button>
      Count: {count}
    </div>
  )
}

export default Counter

Use outside React

// src/utils/vanilla.ts
import { myStore } from '../store'

const count = myStore.getCount() // Get a snapshot of the current value

myStore.setCount(p => p + 1) // Update state

Considerations

  • states is an object of hooks, they can only be called inside React components.
1.0.2

3 months ago

1.0.1

3 months ago

1.0.0

3 months ago

0.1.8

7 months ago

0.1.7

7 months ago

0.1.6

9 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.0.1

10 months ago

0.0.0

10 months ago

0.1.0

3 years ago