react-datocms
Advanced tools
Comparing version 1.4.1 to 1.4.2
@@ -32,4 +32,2 @@ export declare type UpdateData<QueryResult> = { | ||
initialData?: QueryResult | null; | ||
/** Whether the subscription has to be performed or not */ | ||
enabled?: boolean; | ||
/** In case of network errors, the period to wait to reconnect */ | ||
@@ -36,0 +34,0 @@ reconnectionPeriod?: number; |
import { ChannelErrorData } from "../subscribeToQuery"; | ||
declare type QueryListenerOptions<QueryResult, QueryVariables> = { | ||
/** The GraphQL query to subscribe */ | ||
query: string; | ||
declare type OptionalConfigurations<QueryResult, QueryVariables> = { | ||
/** GraphQL variables for the query */ | ||
variables?: QueryVariables; | ||
/** DatoCMS API token to use */ | ||
token: string; | ||
/** If true, the Content Delivery API with draft content will be used */ | ||
@@ -15,4 +11,2 @@ preview?: boolean; | ||
initialData?: QueryResult | null; | ||
/** Whether the subscription has to be performed or not */ | ||
enabled?: boolean; | ||
/** In case of network errors, the period to wait to reconnect */ | ||
@@ -25,2 +19,19 @@ reconnectionPeriod?: number; | ||
}; | ||
declare type EnabledQueryListenerOptions<QueryResult, QueryVariables> = { | ||
/** Whether the subscription has to be performed or not */ | ||
enabled?: true; | ||
/** The GraphQL query to subscribe */ | ||
query: string; | ||
/** DatoCMS API token to use */ | ||
token: string; | ||
} & OptionalConfigurations<QueryResult, QueryVariables>; | ||
declare type DisabledQueryListenerOptions<QueryResult, QueryVariables> = { | ||
/** Whether the subscription has to be performed or not */ | ||
enabled: false; | ||
/** The GraphQL query to subscribe */ | ||
query?: string; | ||
/** DatoCMS API token to use */ | ||
token?: string; | ||
} & OptionalConfigurations<QueryResult, QueryVariables>; | ||
declare type QueryListenerOptions<QueryResult, QueryVariables> = EnabledQueryListenerOptions<QueryResult, QueryVariables> | DisabledQueryListenerOptions<QueryResult, QueryVariables>; | ||
export declare function useQuerySubscription<QueryResult = any, QueryVariables = Record<string, any>>(options: QueryListenerOptions<QueryResult, QueryVariables>): { | ||
@@ -27,0 +38,0 @@ error: ChannelErrorData | null; |
@@ -43,9 +43,10 @@ "use strict"; | ||
function useQuerySubscription(options) { | ||
var initialData = options.initialData, enabled = options.enabled, preview = options.preview, query = options.query, token = options.token, variables = options.variables, environment = options.environment, fetcher = options.fetcher, reconnectionPeriod = options.reconnectionPeriod; | ||
var enabled = options.enabled, initialData = options.initialData; | ||
var _a = react_1.useState(null), error = _a[0], setError = _a[1]; | ||
var _b = react_1.useState(null), data = _b[0], setData = _b[1]; | ||
var _c = react_1.useState(enabled ? "connecting" : "closed"), status = _c[0], setStatus = _c[1]; | ||
var _d = options, preview = _d.preview, query = _d.query, token = _d.token, variables = _d.variables, environment = _d.environment, fetcher = _d.fetcher, reconnectionPeriod = _d.reconnectionPeriod; | ||
use_deep_compare_effect_1.useDeepCompareEffectNoCheck(function () { | ||
if (enabled === false) { | ||
setStatus('closed'); | ||
setStatus("closed"); | ||
return function () { | ||
@@ -93,3 +94,11 @@ // we don't have to perform any uninstall | ||
}; | ||
}, [preview, query, token, variables, environment]); | ||
}, [ | ||
preview, | ||
query, | ||
token, | ||
variables, | ||
environment, | ||
fetcher, | ||
reconnectionPeriod, | ||
]); | ||
return { error: error, status: status, data: data || initialData }; | ||
@@ -96,0 +105,0 @@ } |
{ | ||
"name": "react-datocms", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"types": "dist/index.d.ts", | ||
@@ -9,2 +9,6 @@ "main": "dist/index.js", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/datocms/react-datocms.git" | ||
}, | ||
"license": "MIT", | ||
@@ -11,0 +15,0 @@ "author": "Stefano Verna <s.verna@datocms.com>", |
@@ -61,2 +61,4 @@ # react-datocms | ||
Import `useQuerySubscription` from `react-datocms` and use it inside your components like this: | ||
```js | ||
@@ -70,7 +72,2 @@ const { | ||
## Usage | ||
1. Import `useQuerySubscription` from `react-datocms` and use it inside your components | ||
2. **Important:** Remember to set the `enabled` property, or the hook will simply return the `initialData` you pass! | ||
## Initialization options | ||
@@ -77,0 +74,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
69785
724
0
316