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: bind state methods #3

Merged
merged 3 commits into from
Apr 25, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## Added

- feat: bind state methods #3

## [0.1.0] - 2024-04-25

### Changed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ and open <http://localhost:8080> in your web browser.

You can also try them in codesandbox.io:
[01](https://codesandbox.io/s/github/zustandjs/zustand-valtio/tree/main/examples/01_counter)
[02](https://codesandbox.io/s/github/zustandjs/zustand-valtio/tree/main/examples/02_methods)

## Tweets

Expand Down
4 changes: 2 additions & 2 deletions examples/01_counter/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createWithProxy } from 'zustand-valtio';

const [useCounterState, counterState] = createWithProxy({ count: 0 });
const useCounterState = createWithProxy({ count: 0 });

const Counter = () => {
const count = useCounterState((state) => state.count);
const inc = () => ++counterState.count;
const inc = () => ++useCounterState.proxyState.count;
return (
<>
<div>Count: {count}</div>
Expand Down
23 changes: 23 additions & 0 deletions examples/02_methods/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "example",
"version": "0.0.0",
"private": true,
"type": "commonjs",
"dependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"react": "latest",
"react-dom": "latest",
"react-scripts": "latest",
"typescript": "latest",
"valtio": "latest",
"zustand": "latest",
"zustand-valtio": "latest"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
}
8 changes: 8 additions & 0 deletions examples/02_methods/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>example</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
29 changes: 29 additions & 0 deletions examples/02_methods/src/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createWithProxy } from 'zustand-valtio';

const useCounterState = createWithProxy({
count: 0,
inc() {
this.count++;
},
});

const Counter = () => {
const count = useCounterState((state) => state.count);
const inc = useCounterState((state) => state.inc);
return (
<>
<div>Count: {count}</div>
<button type="button" onClick={inc}>
+1
</button>
</>
);
};

const App = () => (
<div>
<Counter />
</div>
);

export default App;
13 changes: 13 additions & 0 deletions examples/02_methods/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

import App from './app';

const ele = document.getElementById('app');
if (ele) {
createRoot(ele).render(
<StrictMode>
<App />
</StrictMode>,
);
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"test:types": "tsc -p . --noEmit",
"test:types:examples": "tsc -p examples --noEmit",
"test:spec": "vitest run",
"examples:01_counter": "DIR=01_counter vite"
"examples:01_counter": "DIR=01_counter vite",
"examples:02_methods": "DIR=02_methods vite"
},
"keywords": [
"react",
Expand Down
20 changes: 15 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ import { createStore, useStore } from 'zustand';
import { proxy, snapshot, subscribe } from 'valtio/vanilla';
import type { INTERNAL_Snapshot as Snapshot } from 'valtio/vanilla';

export function createWithProxy<T extends object>(initialObject: T) {
export function createWithProxy<T extends object>(
initialObject: T,
): {
<Slice>(selector: (state: Snapshot<T>) => Slice): Slice;
proxyState: T;
} {
const proxyState = proxy(initialObject);
Object.keys(proxyState as object).forEach((key) => {
const fn = (proxyState as Record<string, unknown>)[key];
if (typeof fn === 'function') {
(proxyState as Record<string, unknown>)[key] = fn.bind(proxyState);
}
});
const store = createStore(() => snapshot(proxyState));
const unsubscribe = subscribe(proxyState, () =>
store.setState(snapshot(proxyState), true),
);
subscribe(proxyState, () => store.setState(snapshot(proxyState), true));
const useProxyState = <Slice>(selector: (state: Snapshot<T>) => Slice) =>
useStore(store, selector);
return [useProxyState, proxyState, unsubscribe] as const;
useProxyState.proxyState = proxyState;
return useProxyState;
}
Loading