@prismicio/react 2.0.0-beta.8 → 2.0.2

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.
@@ -71,24 +71,6 @@ export type HookOnlyParameters = {
71
71
  skip?: boolean;
72
72
  };
73
73
 
74
- const getParamHookDependencies = (
75
- params: NonNullable<ClientMethodParameters<"get">[0]>,
76
- ) => {
77
- return [
78
- params.ref,
79
- params.lang,
80
- params.page,
81
- params.after,
82
- params.fetch,
83
- params.pageSize,
84
- params.orderings,
85
- params.fetchLinks,
86
- params.graphQuery,
87
- params.predicates,
88
- params.accessToken,
89
- ];
90
- };
91
-
92
74
  /**
93
75
  * Determines if a value is a `@prismicio/client` params object.
94
76
  *
@@ -157,8 +139,13 @@ export const useStatefulPrismicClientMethod = <
157
139
 
158
140
  React.useEffect(
159
141
  () => {
142
+ // Used to prevent dispatching an action if the hook was cleaned up.
143
+ let didCancel = false;
144
+
160
145
  if (!skip) {
161
- dispatch(["start"]);
146
+ if (!didCancel) {
147
+ dispatch(["start"]);
148
+ }
162
149
 
163
150
  client[methodName]
164
151
  .call(
@@ -167,20 +154,35 @@ export const useStatefulPrismicClientMethod = <
167
154
  ...argsWithoutParams,
168
155
  params,
169
156
  )
170
- .then((result) => dispatch(["succeed", result as TData]))
171
- .catch((error) => dispatch(["fail", error]));
157
+ .then((result) => {
158
+ if (!didCancel) {
159
+ dispatch(["succeed", result as TData]);
160
+ }
161
+ })
162
+ .catch((error) => {
163
+ if (!didCancel) {
164
+ dispatch(["fail", error]);
165
+ }
166
+ });
172
167
  }
168
+
169
+ // Ensure we don't dispatch an action if the hook is cleaned up.
170
+ () => {
171
+ didCancel = true;
172
+ };
173
173
  },
174
- // We must disable exhaustive-deps to optimize providing `params` deps.
174
+ // We must disable exhaustive-deps since we are using
175
+ // JSON.stringify on params (effectively a deep equality check).
176
+ // We want this effect to run again anytime params change.
175
177
  // eslint-disable-next-line react-hooks/exhaustive-deps
176
178
  [
177
179
  client,
178
- skip,
179
180
  methodName,
181
+ skip,
180
182
  // eslint-disable-next-line react-hooks/exhaustive-deps
181
- ...argsWithoutParams,
183
+ JSON.stringify(argsWithoutParams),
182
184
  // eslint-disable-next-line react-hooks/exhaustive-deps
183
- ...getParamHookDependencies(params),
185
+ JSON.stringify(params),
184
186
  ],
185
187
  );
186
188