-
Notifications
You must be signed in to change notification settings - Fork 8
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
immer example for createSliceWithImmer #26
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da75939
immer example for createSliceWithImmer
Ebubekir-Tas faa23c5
formatting
Ebubekir-Tas ef620da
add immer to dependencies
Ebubekir-Tas 43a1cb5
immer example
Ebubekir-Tas 341f129
.
Ebubekir-Tas 6ce615d
update script for immer example in root package.json
Ebubekir-Tas 4830029
chore: package.json in the example
dai-shi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -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,21 @@ | ||
{ | ||
"name": "example", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"react": "latest", | ||
"react-dom": "latest", | ||
"zustand": "latest", | ||
"zustand-slices": "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,59 @@ | ||
import { create } from 'zustand'; | ||
import { withSlices } from 'zustand-slices'; | ||
import { createSliceWithImmer } from 'zustand-slices/immer'; | ||
|
||
const countSlice = createSliceWithImmer({ | ||
name: 'count', | ||
value: { | ||
count: 0, | ||
}, | ||
actions: { | ||
inc: () => (state) => { | ||
state.count += 1; | ||
}, | ||
reset: () => () => ({ count: 0 }), | ||
}, | ||
}); | ||
|
||
const textSlice = createSliceWithImmer({ | ||
name: 'text', | ||
value: 'Hello', | ||
actions: { | ||
updateText: (newText: string) => () => newText, | ||
reset: () => () => 'Hello', | ||
}, | ||
}); | ||
|
||
const useCountStore = create(withSlices(countSlice, textSlice)); | ||
|
||
const Counter = () => { | ||
const { count } = useCountStore((state) => state.count); | ||
const text = useCountStore((state) => state.text); | ||
const { inc, updateText, reset } = useCountStore.getState(); | ||
return ( | ||
<> | ||
<p> | ||
Count: {count} | ||
<button type="button" onClick={inc}> | ||
+1 | ||
</button> | ||
</p> | ||
<p> | ||
<input value={text} onChange={(e) => updateText(e.target.value)} /> | ||
</p> | ||
<p> | ||
<button type="button" onClick={reset}> | ||
Reset | ||
</button> | ||
</p> | ||
</> | ||
); | ||
}; | ||
|
||
const App = () => ( | ||
<div> | ||
<Counter /> | ||
</div> | ||
); | ||
|
||
export default App; |
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" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need
"immer"
too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added immer as a dependency in the examples folder, I need to also add
to the root package.json
but it seems something desynced my local branch to this pull request and I'm hesitant to make any force pushes(fixed)I ran the example locally and it worked fine. Noteworthy that to run it in windows the script is
"examples:04_immer": "set DIR=04_immer&& vite"
edit: I also see stackblitz URLs in the README, but the new
createSliceWithImmer
method right now isn't included in the latest published version on NPM so it isn't yet accessible without a local clone usingnpm link
so the method is not accessible on stackblitz right now