Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(createStore): #138

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.8",
"version": "2.7.9",
"description": "Atomic State is a state management library for React",
"main": "dist/index.js",
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export {
createPersistence,
setAtom,
getActions,
createStore
createStore,
store,
atomicHook
} from './mod'

export { getAtomValue, getAtom, getValue } from './store'
9 changes: 9 additions & 0 deletions src/mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,7 @@ export function createAtomicHook<R>(config: Partial<Atom<R>> = {}) {
R,
{
setPartialvalue: Partial<R> | ((v: Required<R>) => Partial<R>)
setPartial: Partial<R> | ((v: Required<R>) => Partial<R>)
setValue: R | ((v: Required<R>) => R)
reset: any
} & {
Expand All @@ -1417,6 +1418,11 @@ export function createAtomicHook<R>(config: Partial<Atom<R>> = {}) {
dispatch(prev => ({ ...prev, ...args(prev as Required<R>) }))
} else dispatch(prev => ({ ...prev, ...args }))
},
setPartial({ dispatch, args }) {
if (typeof args === 'function') {
dispatch(prev => ({ ...prev, ...args(prev as Required<R>) }))
} else dispatch(prev => ({ ...prev, ...args }))
},
// Can be used with non-object values
setValue({ dispatch, args }) {
// @ts-ignore
Expand Down Expand Up @@ -1477,6 +1483,9 @@ export function createStore<R>(config: Partial<Atom<R>> = {}) {
return useStore
}

export const store = createStore
export const atomicHook = createAtomicHook

/**
* Get an atom's value
*/
Expand Down
Loading