Café is a global state manager for React and vanilla JavaScript
npm i react-cafe
// src/store/index.ts
import { createStore } from "react-cafe";
type MyStore = {
count: number
}
export const myStore = createStore<MyStore>({
count: 0
})
// 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
// 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
npm i vanilla-cafe
import { createStore } from "vanilla-cafe";
export const { set, sub, get } = createStore({
count: 0
})
import { set, sub, get } from '../store';
function increment(){
set.count(p => p + 1)
}
let count = 0;
function handleCountChange(newValue){
count = newValue
}
const unsubscribe = sub.count(handleCountChange)
const count = get.count()