@better-state/react 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +24 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +33 -0
- package/dist/provider.js.map +1 -0
- package/dist/use-better-state.d.ts +24 -0
- package/dist/use-better-state.d.ts.map +1 -0
- package/dist/use-better-state.js +39 -0
- package/dist/use-better-state.js.map +1 -0
- package/dist/use-connection-status.d.ts +14 -0
- package/dist/use-connection-status.d.ts.map +1 -0
- package/dist/use-connection-status.js +23 -0
- package/dist/use-connection-status.js.map +1 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { BetterStateProvider, useBetterStateClient } from "./provider.js";
|
|
2
|
+
export { useBetterState } from "./use-better-state.js";
|
|
3
|
+
export { useConnectionStatus } from "./use-connection-status.js";
|
|
4
|
+
export type { BetterStateProviderProps } from "./provider.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,YAAY,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import { type BetterStateClient, type ClientOptions } from "@better-state/client";
|
|
3
|
+
export interface BetterStateProviderProps {
|
|
4
|
+
url: string;
|
|
5
|
+
options: ClientOptions;
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Provides a Better-State client to the component tree.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* <BetterStateProvider url="http://localhost:3001" options={{ apiKey: 'your-key' }}>
|
|
14
|
+
* <App />
|
|
15
|
+
* </BetterStateProvider>
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function BetterStateProvider({ url, options, children }: BetterStateProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the Better-State client instance from the nearest provider.
|
|
21
|
+
* Useful for calling `client.disconnect()` or accessing advanced APIs.
|
|
22
|
+
*/
|
|
23
|
+
export declare function useBetterStateClient(): BetterStateClient;
|
|
24
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAqC,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAC1E,OAAO,EAAgB,KAAK,iBAAiB,EAAE,KAAK,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAIhG,MAAM,WAAW,wBAAwB;IACvC,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,aAAa,CAAC;IACvB,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,wBAAwB,2CAYvF;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,iBAAiB,CAMxD"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useRef } from "react";
|
|
3
|
+
import { createClient } from "@better-state/client";
|
|
4
|
+
const BetterStateContext = createContext(null);
|
|
5
|
+
/**
|
|
6
|
+
* Provides a Better-State client to the component tree.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* <BetterStateProvider url="http://localhost:3001" options={{ apiKey: 'your-key' }}>
|
|
11
|
+
* <App />
|
|
12
|
+
* </BetterStateProvider>
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function BetterStateProvider({ url, options, children }) {
|
|
16
|
+
const clientRef = useRef(null);
|
|
17
|
+
if (!clientRef.current) {
|
|
18
|
+
clientRef.current = createClient(url, options);
|
|
19
|
+
}
|
|
20
|
+
return (_jsx(BetterStateContext.Provider, { value: clientRef.current, children: children }));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the Better-State client instance from the nearest provider.
|
|
24
|
+
* Useful for calling `client.disconnect()` or accessing advanced APIs.
|
|
25
|
+
*/
|
|
26
|
+
export function useBetterStateClient() {
|
|
27
|
+
const client = useContext(BetterStateContext);
|
|
28
|
+
if (!client) {
|
|
29
|
+
throw new Error("useBetterStateClient must be used within a <BetterStateProvider>");
|
|
30
|
+
}
|
|
31
|
+
return client;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAkB,MAAM,OAAO,CAAC;AAC1E,OAAO,EAAE,YAAY,EAA8C,MAAM,sBAAsB,CAAC;AAEhG,MAAM,kBAAkB,GAAG,aAAa,CAA2B,IAAI,CAAC,CAAC;AAQzE;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,QAAQ,EAA4B;IACtF,MAAM,SAAS,GAAG,MAAM,CAA2B,IAAI,CAAC,CAAC;IAEzD,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;QACvB,SAAS,CAAC,OAAO,GAAG,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;IAED,OAAO,CACL,KAAC,kBAAkB,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,CAAC,OAAO,YAClD,QAAQ,GACmB,CAC/B,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB;IAClC,MAAM,MAAM,GAAG,UAAU,CAAC,kBAAkB,CAAC,CAAC;IAC9C,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;IACtF,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synced state hook — like `useState`, but shared across all connected clients.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```tsx
|
|
6
|
+
* function Counter() {
|
|
7
|
+
* const [count, setCount, updateCount] = useBetterState('counter', 0)
|
|
8
|
+
*
|
|
9
|
+
* return (
|
|
10
|
+
* <div>
|
|
11
|
+
* <p>{count}</p>
|
|
12
|
+
* <button onClick={() => updateCount(n => n + 1)}>+1</button>
|
|
13
|
+
* <button onClick={() => setCount(0)}>Reset</button>
|
|
14
|
+
* </div>
|
|
15
|
+
* )
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param key - Unique state key (shared across all clients)
|
|
20
|
+
* @param initialValue - Default value if this key doesn't exist on the server yet
|
|
21
|
+
* @returns [value, set, update] — current value, setter, and updater function
|
|
22
|
+
*/
|
|
23
|
+
export declare function useBetterState<T>(key: string, initialValue: T): [T, (value: T) => void, (fn: (prev: T) => T) => void];
|
|
24
|
+
//# sourceMappingURL=use-better-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-better-state.d.ts","sourceRoot":"","sources":["../src/use-better-state.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAC9B,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,CAAC,GACd,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,CAuBvD"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useState, useEffect, useRef, useCallback } from "react";
|
|
2
|
+
import { useBetterStateClient } from "./provider.js";
|
|
3
|
+
/**
|
|
4
|
+
* Synced state hook — like `useState`, but shared across all connected clients.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* function Counter() {
|
|
9
|
+
* const [count, setCount, updateCount] = useBetterState('counter', 0)
|
|
10
|
+
*
|
|
11
|
+
* return (
|
|
12
|
+
* <div>
|
|
13
|
+
* <p>{count}</p>
|
|
14
|
+
* <button onClick={() => updateCount(n => n + 1)}>+1</button>
|
|
15
|
+
* <button onClick={() => setCount(0)}>Reset</button>
|
|
16
|
+
* </div>
|
|
17
|
+
* )
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param key - Unique state key (shared across all clients)
|
|
22
|
+
* @param initialValue - Default value if this key doesn't exist on the server yet
|
|
23
|
+
* @returns [value, set, update] — current value, setter, and updater function
|
|
24
|
+
*/
|
|
25
|
+
export function useBetterState(key, initialValue) {
|
|
26
|
+
const client = useBetterStateClient();
|
|
27
|
+
const [value, setValue] = useState(initialValue);
|
|
28
|
+
const stateRef = useRef(null);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const stateObj = client.state(key, initialValue);
|
|
31
|
+
stateRef.current = stateObj;
|
|
32
|
+
const unsub = stateObj.subscribe((v) => setValue(v));
|
|
33
|
+
return unsub;
|
|
34
|
+
}, [client, key]);
|
|
35
|
+
const set = useCallback((v) => stateRef.current?.set(v), []);
|
|
36
|
+
const update = useCallback((fn) => stateRef.current?.update(fn), []);
|
|
37
|
+
return [value, set, update];
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=use-better-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-better-state.js","sourceRoot":"","sources":["../src/use-better-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AAEjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,cAAc,CAC5B,GAAW,EACX,YAAe;IAEf,MAAM,MAAM,GAAG,oBAAoB,EAAE,CAAC;IACtC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAI,YAAY,CAAC,CAAC;IACpD,MAAM,QAAQ,GAAG,MAAM,CAAwB,IAAI,CAAC,CAAC;IAErD,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,MAAM,CAAC,KAAK,CAAI,GAAG,EAAE,YAAY,CAAC,CAAC;QACpD,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAC;QAC5B,MAAM,KAAK,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QACxD,OAAO,KAAK,CAAC;IACf,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;IAElB,MAAM,GAAG,GAAG,WAAW,CACrB,CAAC,CAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,EAClC,EAAE,CACH,CAAC;IAEF,MAAM,MAAM,GAAG,WAAW,CACxB,CAAC,EAAkB,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,CAAC,EACpD,EAAE,CACH,CAAC;IAEF,OAAO,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ConnectionStatus } from "@better-state/client";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the current connection status of the Better-State client.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* function StatusBar() {
|
|
8
|
+
* const status = useConnectionStatus()
|
|
9
|
+
* return <div>{status === 'connected' ? '🟢' : '🔴'} {status}</div>
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function useConnectionStatus(): ConnectionStatus;
|
|
14
|
+
//# sourceMappingURL=use-connection-status.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-connection-status.d.ts","sourceRoot":"","sources":["../src/use-connection-status.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAG7D;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,IAAI,gBAAgB,CAUtD"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
import { useBetterStateClient } from "./provider.js";
|
|
3
|
+
/**
|
|
4
|
+
* Returns the current connection status of the Better-State client.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* function StatusBar() {
|
|
9
|
+
* const status = useConnectionStatus()
|
|
10
|
+
* return <div>{status === 'connected' ? '🟢' : '🔴'} {status}</div>
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function useConnectionStatus() {
|
|
15
|
+
const client = useBetterStateClient();
|
|
16
|
+
const [status, setStatus] = useState(client.status());
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const unsub = client.onStatusChange((s) => setStatus(s));
|
|
19
|
+
return unsub;
|
|
20
|
+
}, [client]);
|
|
21
|
+
return status;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=use-connection-status.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-connection-status.js","sourceRoot":"","sources":["../src/use-connection-status.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAE5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAErD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,mBAAmB;IACjC,MAAM,MAAM,GAAG,oBAAoB,EAAE,CAAC;IACtC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAmB,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;IAExE,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;QACzD,OAAO,KAAK,CAAC;IACf,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAEb,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@better-state/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React hooks for Better-State — real-time synced state with one line of code",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"keywords": ["react", "hooks", "state", "sync", "realtime", "websocket", "better-state"],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/Nathanim1919/better-state.git",
|
|
20
|
+
"directory": "packages/react"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"dev": "tsc --watch",
|
|
28
|
+
"clean": "rm -rf dist"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@better-state/client": ">=0.1.0",
|
|
32
|
+
"react": ">=18.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@better-state/client": "workspace:*",
|
|
36
|
+
"@types/react": "^19.0.0",
|
|
37
|
+
"react": "^19.0.0",
|
|
38
|
+
"typescript": "^5.7.0"
|
|
39
|
+
}
|
|
40
|
+
}
|