Skip to content

Commit

Permalink
immer example for createSliceWithImmer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebubekir-Tas committed Jul 10, 2024
1 parent 29f11af commit da75939
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/04_immer/index.html
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>
21 changes: 21 additions & 0 deletions examples/04_immer/package.json
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"
}
}
59 changes: 59 additions & 0 deletions examples/04_immer/src/app.tsx
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;
10 changes: 10 additions & 0 deletions examples/04_immer/src/main.tsx
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>,
);
14 changes: 14 additions & 0 deletions examples/04_immer/tsconfig.json
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"
}
}

0 comments on commit da75939

Please sign in to comment.