@context-query/react 0.1.0 → 0.2.0-dev.1
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/context.d.ts +0 -3
- package/dist/hooks.d.ts +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/provider.d.ts +6 -9
- package/package.json +2 -2
package/dist/context.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import type { ContextQueryStore, TStateImpl } from "@context-query/core";
|
|
2
2
|
export declare const createContextQuery: <TState extends TStateImpl>() => {
|
|
3
3
|
StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
|
|
4
|
-
ContextQuerySubscriptionContext: import("react").Context<{
|
|
5
|
-
subscribe: ContextQueryStore<TState>["subscribe"] | null;
|
|
6
|
-
} | null>;
|
|
7
4
|
};
|
package/dist/hooks.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { TStateImpl } from "@context-query/core";
|
|
2
2
|
import { createContextQuery } from "./context";
|
|
3
|
-
export declare function createUseContextQuery<TState extends TStateImpl>(contexts: ReturnType<typeof createContextQuery<TState>>): <TKey extends keyof TState>(
|
|
3
|
+
export declare function createUseContextQuery<TState extends TStateImpl>(contexts: ReturnType<typeof createContextQuery<TState>>): <TKey extends keyof TState>(keys?: TKey[]) => readonly [{ [K in TKey]: TState[K]; }, (value: { [K in TKey]: TState[K]; } | ((prev: { [K in TKey]: TState[K]; }) => { [K in TKey]: TState[K]; })) => void];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { TStateImpl } from "@context-query/core";
|
|
2
|
-
export declare function createContextQuery<TState extends TStateImpl>(): {
|
|
3
|
-
Provider: import("react").FC<
|
|
4
|
-
|
|
2
|
+
export declare function createContextQuery<TState extends TStateImpl>(initialState: TState): {
|
|
3
|
+
Provider: import("react").FC<{
|
|
4
|
+
children?: import("react").ReactNode | undefined;
|
|
5
|
+
}>;
|
|
6
|
+
useContextQuery: <TKey extends keyof TState>(keys?: TKey[] | undefined) => readonly [{ [K in TKey]: TState[K]; }, (value: { [K in TKey]: TState[K]; } | ((prev: { [K in TKey]: TState[K]; }) => { [K in TKey]: TState[K]; })) => void];
|
|
7
|
+
updateState: (state: TState | ((prev: TState) => TState)) => void;
|
|
8
|
+
setState: <TKey extends keyof TState>(key: TKey, value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => void;
|
|
5
9
|
};
|
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{useState as t,useEffect as e,useCallback as s,useContext as r,createContext as n}from"react";import{jsx as i}from"react/jsx-runtime";var o,a={};var c=(o||(o=1,a.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const e={...this.state};this.state={...t},Object.keys(t).forEach((t=>{Object.is(e[t],this.state[t])||this.notifyListeners(t)}))}setState(t,e){Object.is(this.state[t],e)||(this.state={...this.state,[t]:e},this.notifyListeners(t))}notifyListeners(t){const e=this.listeners.get(t);if(e){const s=this.state[t];e.forEach((t=>t(s)))}}subscribe(t,e){const s=s=>{e(t,s)};return this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(s),{unsubscribe:()=>{const e=this.listeners.get(t);e&&(e.delete(s),0===e.size&&this.listeners.delete(t))}}}}),a);function u(n){const{StoreContext:i}=n;return n=>{const o=(()=>{const t=r(i);if(!t)throw new Error("useContextQuery must be used within a ContextQueryProvider");return t})(),a=n??Object.keys(o.getState()),c=()=>a.reduce(((t,e)=>({...t,[e]:o.getStateByKey(e)})),{}),[u,h]=t(c);e((()=>{const t=(t,e)=>{h((s=>({...s,[t]:e})))},e=a.map((e=>o.subscribe(e,t)));return()=>{e.forEach((t=>t.unsubscribe()))}}),[a,o]);return[u,s((t=>{const e=c(),s="function"==typeof t?t(e):t;Object.entries(s).forEach((([t,e])=>{o.setState(t,e)}))}),[a,o])]}}function h(t){const e={StoreContext:n(null)},s=((t,e)=>{const{StoreContext:s}=t;return function({children:t}){return i(s.Provider,{value:e,children:t})}})(e,t);return{Provider:s,contexts:e}}function f(t){const e=new c.ContextQueryStore(t),{Provider:s,contexts:r}=h(e);return{Provider:s,useContextQuery:u(r),updateState:t=>{if("function"==typeof t){const s=t,r=e.getState();e.updateState(s(r))}else e.updateState(t)},setState:(t,s)=>{if("function"==typeof s){const r=s,n=e.getStateByKey(t);e.setState(t,r(n))}else e.setState(t,s)}}}export{f as createContextQuery};
|
|
2
2
|
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../../core/dist/index.js","../src/
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../../core/dist/index.js","../src/hooks.ts","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["\"use strict\";exports.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const s={...this.state};this.state={...t};Object.keys(t).forEach((t=>{Object.is(s[t],this.state[t])||this.notifyListeners(t)}))}setState(t,s){Object.is(this.state[t],s)||(this.state={...this.state,[t]:s},this.notifyListeners(t))}notifyListeners(t){const s=this.listeners.get(t);if(s){const e=this.state[t];s.forEach((t=>t(e)))}}subscribe(t,s){const e=e=>{s(t,e)};this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(e);return{unsubscribe:()=>{const s=this.listeners.get(t);s&&(s.delete(e),0===s.size&&this.listeners.delete(t))}}}};\n//# sourceMappingURL=index.js.map\n","import { TStateImpl } from \"@context-query/core\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport function createUseContextQuery<TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>\n) {\n const { StoreContext } = contexts;\n\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextQuery must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TState>(keys?: TKey[]) => {\n const store = useStore();\n const localKeys = keys ?? (Object.keys(store.getState()) as TKey[]);\n\n type StateSubset = { [K in TKey]: TState[K] };\n\n const getStateSubset = (): StateSubset => {\n return localKeys.reduce(\n (acc, key) => ({ ...acc, [key]: store.getStateByKey(key) }),\n {} as StateSubset\n );\n };\n\n const [state, setLocalState] = useState<StateSubset>(getStateSubset);\n\n useEffect(() => {\n const handleChange = (key: TKey, newValue: TState[TKey]) => {\n setLocalState((prev) => ({ ...prev, [key]: newValue }));\n };\n\n const subscriptions = localKeys.map((key) =>\n store.subscribe(key, handleChange)\n );\n\n return () => {\n subscriptions.forEach((sub) => sub.unsubscribe());\n };\n }, [localKeys, store]);\n\n const setState = useCallback(\n (value: StateSubset | ((prev: StateSubset) => StateSubset)) => {\n const currentValue = getStateSubset();\n const updatedValue =\n typeof value === \"function\"\n ? (value as Function)(currentValue)\n : value;\n\n Object.entries(updatedValue).forEach(([k, v]) => {\n store.setState(k as TKey, v as TState[TKey]);\n });\n },\n [localKeys, store]\n );\n\n return [state, setState] as const;\n };\n}\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { FC, PropsWithChildren } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport type ProviderProps<TState extends TStateImpl> = PropsWithChildren;\n\nexport const createContextQueryProvider = <TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>,\n store: ContextQueryStore<TState>\n): FC<ProviderProps<TState>> => {\n const { StoreContext } = contexts;\n\n return function ContextQueryProvider({ children }: ProviderProps<TState>) {\n return (\n <StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n );\n };\n};\n\nexport function createReactContextQuery<TState extends TStateImpl>(\n store: ContextQueryStore<TState>\n) {\n const contexts = createContextQuery<TState>();\n const ContextQueryProvider = createContextQueryProvider<TState>(\n contexts,\n store\n );\n\n return {\n Provider: ContextQueryProvider,\n contexts,\n };\n}\n","import type { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createContextQuery = <TState extends TStateImpl>() => {\n const StoreContext = createContext<ContextQueryStore<TState> | null>(null);\n return {\n StoreContext,\n };\n};\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createUseContextQuery } from \"./hooks\";\nimport { createReactContextQuery } from \"./provider\";\n\nexport function createContextQuery<TState extends TStateImpl>(\n initialState: TState\n) {\n const store = new ContextQueryStore<TState>(initialState);\n\n const { Provider, contexts } = createReactContextQuery<TState>(store);\n const useContextQuery = createUseContextQuery<TState>(contexts);\n\n const updateState = (state: TState | ((prev: TState) => TState)) => {\n if (typeof state === \"function\") {\n const updateFn = state as (prev: TState) => TState;\n const currentValue = store.getState();\n store.updateState(updateFn(currentValue));\n } else {\n store.updateState(state);\n }\n };\n\n const setState = <TKey extends keyof TState>(\n key: TKey,\n value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])\n ) => {\n if (typeof value === \"function\") {\n const updateFn = value as (prev: TState[TKey]) => TState[TKey];\n const currentValue = store.getStateByKey(key);\n store.setState(key, updateFn(currentValue));\n } else {\n store.setState(key, value);\n }\n };\n\n return {\n Provider,\n useContextQuery,\n updateState,\n setState,\n };\n}\n"],"names":["dist","ContextQueryStore","state","listeners","constructor","t","this","Map","getState","getStateByKey","updateState","s","Object","keys","forEach","is","notifyListeners","setState","get","e","subscribe","has","set","Set","add","unsubscribe","delete","size","createUseContextQuery","contexts","StoreContext","store","useContext","Error","useStore","localKeys","getStateSubset","reduce","acc","key","setLocalState","useState","useEffect","handleChange","newValue","prev","subscriptions","map","sub","useCallback","value","currentValue","updatedValue","entries","k","v","createReactContextQuery","createContext","ContextQueryProvider","children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AA8BC,GA4BpDnB,YA1BmBR,IACnB,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMgE,EAAWhE,EACXiD,EAAepB,EAAMvB,WAC3BuB,EAAMrB,YAAYwD,EAASf,SAE3BpB,EAAMrB,YAAYR,IAqBpBe,SAjBe,CACfsB,EACAW,KAEA,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMgB,EAAWhB,EACXC,EAAepB,EAAMtB,cAAc8B,GACzCR,EAAMd,SAASsB,EAAK2B,EAASf,SAE7BpB,EAAMd,SAASsB,EAAKW,IAU1B"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var t=require("react"),
|
|
1
|
+
"use strict";var t,e=require("react"),s=require("react/jsx-runtime"),r={};var n=(t||(t=1,r.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const e={...this.state};this.state={...t},Object.keys(t).forEach((t=>{Object.is(e[t],this.state[t])||this.notifyListeners(t)}))}setState(t,e){Object.is(this.state[t],e)||(this.state={...this.state,[t]:e},this.notifyListeners(t))}notifyListeners(t){const e=this.listeners.get(t);if(e){const s=this.state[t];e.forEach((t=>t(s)))}}subscribe(t,e){const s=s=>{e(t,s)};return this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(s),{unsubscribe:()=>{const e=this.listeners.get(t);e&&(e.delete(s),0===e.size&&this.listeners.delete(t))}}}}),r);function i(t){const{StoreContext:s}=t;return t=>{const r=(()=>{const t=e.useContext(s);if(!t)throw new Error("useContextQuery must be used within a ContextQueryProvider");return t})(),n=t??Object.keys(r.getState()),i=()=>n.reduce(((t,e)=>({...t,[e]:r.getStateByKey(e)})),{}),[o,a]=e.useState(i);e.useEffect((()=>{const t=(t,e)=>{a((s=>({...s,[t]:e})))},e=n.map((e=>r.subscribe(e,t)));return()=>{e.forEach((t=>t.unsubscribe()))}}),[n,r]);return[o,e.useCallback((t=>{const e=i(),s="function"==typeof t?t(e):t;Object.entries(s).forEach((([t,e])=>{r.setState(t,e)}))}),[n,r])]}}function o(t){const r={StoreContext:e.createContext(null)},n=((t,e)=>{const{StoreContext:r}=t;return function({children:t}){return s.jsx(r.Provider,{value:e,children:t})}})(r,t);return{Provider:n,contexts:r}}exports.createContextQuery=function(t){const e=new n.ContextQueryStore(t),{Provider:s,contexts:r}=o(e);return{Provider:s,useContextQuery:i(r),updateState:t=>{if("function"==typeof t){const s=t,r=e.getState();e.updateState(s(r))}else e.updateState(t)},setState:(t,s)=>{if("function"==typeof s){const r=s,n=e.getStateByKey(t);e.setState(t,r(n))}else e.setState(t,s)}}};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../core/dist/index.js","../src/
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../core/dist/index.js","../src/hooks.ts","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["\"use strict\";exports.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const s={...this.state};this.state={...t};Object.keys(t).forEach((t=>{Object.is(s[t],this.state[t])||this.notifyListeners(t)}))}setState(t,s){Object.is(this.state[t],s)||(this.state={...this.state,[t]:s},this.notifyListeners(t))}notifyListeners(t){const s=this.listeners.get(t);if(s){const e=this.state[t];s.forEach((t=>t(e)))}}subscribe(t,s){const e=e=>{s(t,e)};this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(e);return{unsubscribe:()=>{const s=this.listeners.get(t);s&&(s.delete(e),0===s.size&&this.listeners.delete(t))}}}};\n//# sourceMappingURL=index.js.map\n","import { TStateImpl } from \"@context-query/core\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport function createUseContextQuery<TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>\n) {\n const { StoreContext } = contexts;\n\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextQuery must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TState>(keys?: TKey[]) => {\n const store = useStore();\n const localKeys = keys ?? (Object.keys(store.getState()) as TKey[]);\n\n type StateSubset = { [K in TKey]: TState[K] };\n\n const getStateSubset = (): StateSubset => {\n return localKeys.reduce(\n (acc, key) => ({ ...acc, [key]: store.getStateByKey(key) }),\n {} as StateSubset\n );\n };\n\n const [state, setLocalState] = useState<StateSubset>(getStateSubset);\n\n useEffect(() => {\n const handleChange = (key: TKey, newValue: TState[TKey]) => {\n setLocalState((prev) => ({ ...prev, [key]: newValue }));\n };\n\n const subscriptions = localKeys.map((key) =>\n store.subscribe(key, handleChange)\n );\n\n return () => {\n subscriptions.forEach((sub) => sub.unsubscribe());\n };\n }, [localKeys, store]);\n\n const setState = useCallback(\n (value: StateSubset | ((prev: StateSubset) => StateSubset)) => {\n const currentValue = getStateSubset();\n const updatedValue =\n typeof value === \"function\"\n ? (value as Function)(currentValue)\n : value;\n\n Object.entries(updatedValue).forEach(([k, v]) => {\n store.setState(k as TKey, v as TState[TKey]);\n });\n },\n [localKeys, store]\n );\n\n return [state, setState] as const;\n };\n}\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { FC, PropsWithChildren } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport type ProviderProps<TState extends TStateImpl> = PropsWithChildren;\n\nexport const createContextQueryProvider = <TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>,\n store: ContextQueryStore<TState>\n): FC<ProviderProps<TState>> => {\n const { StoreContext } = contexts;\n\n return function ContextQueryProvider({ children }: ProviderProps<TState>) {\n return (\n <StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n );\n };\n};\n\nexport function createReactContextQuery<TState extends TStateImpl>(\n store: ContextQueryStore<TState>\n) {\n const contexts = createContextQuery<TState>();\n const ContextQueryProvider = createContextQueryProvider<TState>(\n contexts,\n store\n );\n\n return {\n Provider: ContextQueryProvider,\n contexts,\n };\n}\n","import type { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createContextQuery = <TState extends TStateImpl>() => {\n const StoreContext = createContext<ContextQueryStore<TState> | null>(null);\n return {\n StoreContext,\n };\n};\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createUseContextQuery } from \"./hooks\";\nimport { createReactContextQuery } from \"./provider\";\n\nexport function createContextQuery<TState extends TStateImpl>(\n initialState: TState\n) {\n const store = new ContextQueryStore<TState>(initialState);\n\n const { Provider, contexts } = createReactContextQuery<TState>(store);\n const useContextQuery = createUseContextQuery<TState>(contexts);\n\n const updateState = (state: TState | ((prev: TState) => TState)) => {\n if (typeof state === \"function\") {\n const updateFn = state as (prev: TState) => TState;\n const currentValue = store.getState();\n store.updateState(updateFn(currentValue));\n } else {\n store.updateState(state);\n }\n };\n\n const setState = <TKey extends keyof TState>(\n key: TKey,\n value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])\n ) => {\n if (typeof value === \"function\") {\n const updateFn = value as (prev: TState[TKey]) => TState[TKey];\n const currentValue = store.getStateByKey(key);\n store.setState(key, updateFn(currentValue));\n } else {\n store.setState(key, value);\n }\n };\n\n return {\n Provider,\n useContextQuery,\n updateState,\n setState,\n };\n}\n"],"names":["dist","ContextQueryStore","state","listeners","constructor","t","this","Map","getState","getStateByKey","updateState","s","Object","keys","forEach","is","notifyListeners","setState","get","e","subscribe","has","set","Set","add","unsubscribe","delete","size","createUseContextQuery","contexts","StoreContext","store","useContext","Error","useStore","localKeys","getStateSubset","reduce","acc","key","setLocalState","useState","useEffect","handleChange","newValue","prev","subscriptions","map","sub","useCallback","value","currentValue","updatedValue","entries","k","v","createReactContextQuery","createContext","ContextQueryProvider","children","_jsx","jsx","Provider","createContextQueryProvider","initialState","useContextQu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DnB,YA1BmBR,IACnB,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMgE,EAAWhE,EACXiD,EAAepB,EAAMvB,WAC3BuB,EAAMrB,YAAYwD,EAASf,SAE3BpB,EAAMrB,YAAYR,IAqBpBe,SAjBe,CACfsB,EACAW,KAEA,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMgB,EAAWhB,EACXC,EAAepB,EAAMtB,cAAc8B,GACzCR,EAAMd,SAASsB,EAAK2B,EAASf,SAE7BpB,EAAMd,SAASsB,EAAKW,IAU1B"}
|
package/dist/provider.d.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import { ContextQueryStore, TStateImpl } from "@context-query/core";
|
|
2
2
|
import { FC, PropsWithChildren } from "react";
|
|
3
3
|
import { createContextQuery } from "./context";
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
export type ProviderProps<TState extends TStateImpl> = PropsWithChildren;
|
|
5
|
+
export declare const createContextQueryProvider: <TState extends TStateImpl>(contexts: ReturnType<typeof createContextQuery<TState>>, store: ContextQueryStore<TState>) => FC<ProviderProps<TState>>;
|
|
6
|
+
export declare function createReactContextQuery<TState extends TStateImpl>(store: ContextQueryStore<TState>): {
|
|
7
|
+
Provider: FC<{
|
|
8
|
+
children?: import("react").ReactNode | undefined;
|
|
9
|
+
}>;
|
|
10
10
|
contexts: {
|
|
11
11
|
StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
|
|
12
|
-
ContextQuerySubscriptionContext: import("react").Context<{
|
|
13
|
-
subscribe: (<TKey extends keyof TState>(key: TKey, listener: (key: TKey, value: TState[TKey]) => void) => import("@context-query/core").Subscription) | null;
|
|
14
|
-
} | null>;
|
|
15
12
|
};
|
|
16
13
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@context-query/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-dev.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "React bindings for ContextQuery",
|
|
6
6
|
"author": "Minyeoung Seo <tjalsdud89@naver.com>",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"react": "^18.0.0 || ^19.0.0"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@context-query/core": "
|
|
33
|
+
"@context-query/core": "0.2.0-dev.1",
|
|
34
34
|
"react": "^18.3.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|