Skip to content

Commit

Permalink
Merge pull request #137 from atomic-state/features/createStore
Browse files Browse the repository at this point in the history
enh(createStore):
  • Loading branch information
danybeltran committed May 11, 2024
2 parents f436a0c + c9d5381 commit 5e29013
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "atomic-state",
"version": "2.7.7",
"version": "2.7.8",
"description": "Atomic State is a state management library for React",
"main": "dist/index.js",
"repository": {
Expand Down
28 changes: 22 additions & 6 deletions src/mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1451,14 +1451,30 @@ export function createAtomicHook<R>(config: Partial<Atom<R>> = {}) {
export function createStore<R>(config: Partial<Atom<R>> = {}) {
const use$tore = createAtomicHook(config)

return function useStore() {
const [storeValue, storeActions] = use$tore()
const use$toreValue = atom(config as Atom<R>)

return {
...storeValue,
...storeActions
}
function useStore() {
const store = (
_isDefined((config as any)?.get) ? useValue(use$toreValue) : use$tore()
) as ReturnType<typeof use$tore>

const storeValue = store?.[0]

const storeActions = store?.[1]

return Array.isArray(store)
? {
...storeValue,
...storeActions
}
: (store as typeof config extends Selector
? R
: typeof storeValue & typeof storeActions)
}

useStore.atom = use$tore.atom

return useStore
}

/**
Expand Down

0 comments on commit 5e29013

Please sign in to comment.