-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: atomWithActions & create * update CHANGELOG * little more completeness
- Loading branch information
Showing
10 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ | |
|
||
## [Unreleased] | ||
|
||
### Added | ||
|
||
- feat: atomWithActions & create #5 | ||
|
||
## [0.4.0] - 2024-05-27 | ||
|
||
### Changed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<title>example</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "example", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"jotai": "latest", | ||
"jotai-zustand": "latest", | ||
"react": "latest", | ||
"react-dom": "latest", | ||
"zustand": "latest" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "latest", | ||
"@types/react-dom": "latest", | ||
"typescript": "latest", | ||
"vite": "latest" | ||
}, | ||
"scripts": { | ||
"dev": "vite" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useCallback } from 'react'; | ||
import { create } from 'jotai-zustand'; | ||
|
||
const useCountStore = create( | ||
{ | ||
count: 0, | ||
}, | ||
(set) => ({ | ||
inc: () => set((prev) => ({ count: prev.count + 1 })), | ||
}), | ||
); | ||
|
||
const Counter = () => { | ||
const count = useCountStore(useCallback((state) => state.count, [])); | ||
const inc = useCountStore(useCallback((state) => state.inc, [])); | ||
|
||
return ( | ||
<> | ||
count: {count} | ||
<button onClick={inc}>inc</button> | ||
</> | ||
); | ||
}; | ||
|
||
export default function App() { | ||
return ( | ||
<div className="App"> | ||
<Counter /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { StrictMode } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
import App from './app'; | ||
|
||
createRoot(document.getElementById('root')!).render( | ||
<StrictMode> | ||
<App /> | ||
</StrictMode>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"target": "es2018", | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "bundler", | ||
"skipLibCheck": true, | ||
"allowJs": true, | ||
"noUncheckedIndexedAccess": true, | ||
"exactOptionalPropertyTypes": true, | ||
"jsx": "react-jsx" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { atom } from 'jotai/vanilla'; | ||
|
||
export function atomWithActions<State extends object, Actions extends object>( | ||
initialState: State, | ||
createActions: ( | ||
set: ( | ||
partial: Partial<State> | ((prev: State) => Partial<State>), | ||
replace?: boolean, | ||
) => void, | ||
get: () => State, | ||
) => Actions, | ||
) { | ||
const stateAtom = atom(initialState); | ||
if (process.env.NODE_ENV !== 'production') { | ||
stateAtom.debugPrivate = true; | ||
} | ||
const actionsAtom = atom( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(_get, { setSelf }: any) => { | ||
const actions = createActions( | ||
(partial, replace) => setSelf({ type: 'set', partial, replace }), | ||
() => setSelf({ type: 'get' }), | ||
); | ||
return actions; | ||
}, | ||
( | ||
get, | ||
set, | ||
arg: | ||
| { type: 'get' } | ||
| { | ||
type: 'set'; | ||
partial: Partial<State> | ((prev: State) => Partial<State>); | ||
replace: boolean | undefined; | ||
}, | ||
) => { | ||
const state = get(stateAtom); | ||
if (arg.type === 'get') { | ||
return state; | ||
} | ||
const { partial, replace } = arg; | ||
const nextState = | ||
typeof partial === 'function' ? partial(state) : partial; | ||
if (!Object.is(nextState, state)) { | ||
set( | ||
stateAtom, | ||
replace ? (nextState as State) : Object.assign({}, state, nextState), | ||
); | ||
} | ||
}, | ||
); | ||
if (process.env.NODE_ENV !== 'production') { | ||
actionsAtom.debugPrivate = true; | ||
} | ||
const derivedAtom = atom((get) => ({ | ||
...get(stateAtom), | ||
...get(actionsAtom), | ||
})); | ||
return derivedAtom; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useMemo } from 'react'; | ||
import { atom, createStore } from 'jotai/vanilla'; | ||
import { useAtomValue } from 'jotai/react'; | ||
|
||
import { atomWithActions } from './atomWithActions.js'; | ||
|
||
export function create<State extends object, Actions extends object>( | ||
initialState: State, | ||
createActions: ( | ||
set: (partial: Partial<State> | ((prev: State) => Partial<State>)) => void, | ||
get: () => State, | ||
) => Actions, | ||
) { | ||
const store = createStore(); | ||
const theAtom = atomWithActions(initialState, createActions); | ||
const useStore = <Slice>(selector: (state: State & Actions) => Slice) => { | ||
const derivedAtom = useMemo( | ||
() => atom((get) => selector(get(theAtom))), | ||
[selector], | ||
); | ||
return useAtomValue(derivedAtom, { store }); | ||
}; | ||
const useStoreWithGetState = useStore as typeof useStore & { | ||
getState: () => State & Actions; | ||
}; | ||
useStoreWithGetState.getState = () => store.get(theAtom); | ||
return useStoreWithGetState; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { atomWithStore } from './atomWithStore.js'; | ||
export { atomWithActions } from './atomWithActions.js'; | ||
export { create } from './create.js'; |