Simple React store based on useSyncExternalStore
npm install react-atomic-storebash
npm install react-atomic-store
`
$3
`tsx
import { createStore } from 'react-atomic-store'
const { useStore } = createStore({ foo: 1, bar: true })
function Bar() {
const { bar, setBar } = useStore()
return (
<>
bar: {bar.toString()}
onClick={() => {
setBar(v => !v)
}}
>
change bar
>
)
}
function App() {
const { foo, setFoo, bar } = useStore()
return (
foo: {foo}
onClick={() => {
setFoo(foo + 1)
}}
>
add foo
)
}
`
$3
Only one api: createStore
`ts
const { useStore, getStoreMethods, getStoreState, getStateSnapshot, subscribeStore } = createStore({
foo: 1,
bar: false,
})
`
Return value of createStore:
- useStore
get current store state and get/set methods
`tsx
function MyComp() {
const { foo, setFoo, getFoo, bar, setBar, getBar } = useStore()
return (
onClick={() => {
setFoo()
}}
>
{foo}
- getStoreMethods`