Skip to content

Commit 7275c19

Browse files
authored
[graphiql-react] create instance of new HistoryStore and new StorageAPI only on mount, use function with useState (#3743)
* create instance of `new HistoryStore` and `new StorageAPI` only on mount, use function with `useState` * aa
1 parent 26cd829 commit 7275c19

File tree

3 files changed

+35
-63
lines changed

3 files changed

+35
-63
lines changed

.changeset/pretty-panthers-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphiql/react': patch
3+
---
4+
5+
create instance of `new HistoryStore` and `new StorageAPI` only on mount, use function with `useState`

packages/graphiql-react/src/history/context.tsx

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HistoryStore, QueryStoreItem, StorageAPI } from '@graphiql/toolkit';
2-
import { ReactNode, useCallback, useMemo, useRef, useState } from 'react';
2+
import { ReactNode, useMemo, useState } from 'react';
33

44
import { useStorageContext } from '../storage';
55
import { createContextHook, createNullableContext } from '../utility/context';
@@ -87,77 +87,44 @@ export type HistoryContextProviderProps = {
8787
* any additional props they added for their needs (i.e., build their own functions that may save
8888
* to a backend instead of localStorage and might need an id property added to the QueryStoreItem)
8989
*/
90-
export function HistoryContextProvider(props: HistoryContextProviderProps) {
90+
export function HistoryContextProvider({
91+
maxHistoryLength = DEFAULT_HISTORY_LENGTH,
92+
children,
93+
}: HistoryContextProviderProps) {
9194
const storage = useStorageContext();
92-
const historyStore = useRef(
93-
new HistoryStore(
95+
const [historyStore] = useState(
96+
() =>
9497
// Fall back to a noop storage when the StorageContext is empty
95-
storage || new StorageAPI(null),
96-
props.maxHistoryLength || DEFAULT_HISTORY_LENGTH,
97-
),
98+
new HistoryStore(storage || new StorageAPI(null), maxHistoryLength),
9899
);
99-
const [items, setItems] = useState(historyStore.current?.queries || []);
100-
101-
const addToHistory: HistoryContextType['addToHistory'] = useCallback(
102-
(operation: QueryStoreItem) => {
103-
historyStore.current?.updateHistory(operation);
104-
setItems(historyStore.current.queries);
105-
},
106-
[],
107-
);
108-
109-
const editLabel: HistoryContextType['editLabel'] = useCallback(
110-
(operation: QueryStoreItem, index?: number) => {
111-
historyStore.current.editLabel(operation, index);
112-
setItems(historyStore.current.queries);
113-
},
114-
[],
115-
);
116-
117-
const toggleFavorite: HistoryContextType['toggleFavorite'] = useCallback(
118-
(operation: QueryStoreItem) => {
119-
historyStore.current.toggleFavorite(operation);
120-
setItems(historyStore.current.queries);
121-
},
122-
[],
123-
);
124-
125-
const setActive: HistoryContextType['setActive'] = useCallback(
126-
(item: QueryStoreItem) => {
127-
return item;
128-
},
129-
[],
130-
);
131-
132-
const deleteFromHistory: HistoryContextType['deleteFromHistory'] =
133-
useCallback((item: QueryStoreItem, clearFavorites = false) => {
134-
historyStore.current.deleteHistory(item, clearFavorites);
135-
setItems(historyStore.current.queries);
136-
}, []);
100+
const [items, setItems] = useState(() => historyStore.queries || []);
137101

138102
const value = useMemo<HistoryContextType>(
139103
() => ({
140-
addToHistory,
141-
editLabel,
104+
addToHistory(operation) {
105+
historyStore.updateHistory(operation);
106+
setItems(historyStore.queries);
107+
},
108+
editLabel(operation, index) {
109+
historyStore.editLabel(operation, index);
110+
setItems(historyStore.queries);
111+
},
142112
items,
143-
toggleFavorite,
144-
setActive,
145-
deleteFromHistory,
113+
toggleFavorite(operation) {
114+
historyStore.toggleFavorite(operation);
115+
setItems(historyStore.queries);
116+
},
117+
setActive: item => item,
118+
deleteFromHistory(item, clearFavorites) {
119+
historyStore.deleteHistory(item, clearFavorites);
120+
setItems(historyStore.queries);
121+
},
146122
}),
147-
[
148-
addToHistory,
149-
editLabel,
150-
items,
151-
toggleFavorite,
152-
setActive,
153-
deleteFromHistory,
154-
],
123+
[items, historyStore],
155124
);
156125

157126
return (
158-
<HistoryContext.Provider value={value}>
159-
{props.children}
160-
</HistoryContext.Provider>
127+
<HistoryContext.Provider value={value}>{children}</HistoryContext.Provider>
161128
);
162129
}
163130

packages/graphiql-react/src/storage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type StorageContextProviderProps = {
1212
children: ReactNode;
1313
/**
1414
* Provide a custom storage API.
15-
* @default `localStorage``
15+
* @default `localStorage`
1616
* @see {@link https://graphiql-test.netlify.app/typedoc/modules/graphiql_toolkit.html#storage-2|API docs}
1717
* for details on the required interface.
1818
*/
@@ -21,7 +21,7 @@ export type StorageContextProviderProps = {
2121

2222
export function StorageContextProvider(props: StorageContextProviderProps) {
2323
const isInitialRender = useRef(true);
24-
const [storage, setStorage] = useState(new StorageAPI(props.storage));
24+
const [storage, setStorage] = useState(() => new StorageAPI(props.storage));
2525

2626
useEffect(() => {
2727
if (isInitialRender.current) {

0 commit comments

Comments
 (0)