Skip to content

Commit b152adf

Browse files
committed
Move the scratchFetchApi into a ref
Now I've pushed this into the component, it is safer to store it as a ref rather than in a variable.
1 parent 0f0044d commit b152adf

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/components/ScratchEditor/ScratchEditor.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import scratchProjectSave from "../../utils/scratchProjectSave.js";
2-
import { useCallback } from "react";
2+
import { useCallback, useRef } from "react";
33

44
import WrapperdScratchGui from "./WrappedScratchGui.jsx";
55
import { postScratchGuiEvent } from "./events.js";
@@ -30,13 +30,13 @@ const handleScratchGuiAlert = (alertType) => {
3030
}
3131
};
3232

33-
let scratchFetchApi = null;
34-
3533
const ScratchEditor = ({ projectId, locale, apiUrl, accessToken }) => {
34+
const scratchFetchApiRef = useRef(null);
35+
3636
const handleUpdateProjectData = useCallback(
3737
async (currentProjectId, vmState, params) => {
3838
return scratchProjectSave({
39-
scratchFetchApi,
39+
scratchFetchApi: scratchFetchApiRef.current,
4040
apiUrl,
4141
currentProjectId,
4242
vmState,
@@ -55,9 +55,9 @@ const ScratchEditor = ({ projectId, locale, apiUrl, accessToken }) => {
5555
assetHost={`${apiUrl}/api/scratch/assets`}
5656
basePath={`${process.env.ASSETS_URL}/scratch-gui/`}
5757
onStorageInit={(storage) => {
58-
scratchFetchApi = storage.scratchFetch;
58+
scratchFetchApiRef.current = storage.scratchFetch;
5959
if (accessToken) {
60-
scratchFetchApi.setMetadata("Authorization", accessToken);
60+
scratchFetchApiRef.current.setMetadata("Authorization", accessToken);
6161
}
6262
}}
6363
onUpdateProjectData={handleUpdateProjectData}

0 commit comments

Comments
 (0)