Skip to content

Commit 37dbd92

Browse files
notthatjenBosEriko
andauthored
Hotfix: Undefined filter method (#23)
* hotfix for filter error * hotfix for useContext Co-authored-by: BosEriko <[email protected]>
1 parent 77209a8 commit 37dbd92

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/useChunk.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-check
2-
import { useContext, useEffect, useState } from "react";
2+
import { useContext, useEffect, useState, useMemo } from "react";
33

44
import { api } from "./api";
55
import { EditmodeContext } from "./EditmodeContext";
@@ -11,6 +11,13 @@ export function useChunk(defaultContent, { identifier, type }) {
1111
const { projectId, defaultChunks } = useContext(EditmodeContext);
1212
const [[error, chunk], setResponse] = useState([undefined, undefined]);
1313
const contentKey = defaultContent ? computeContentKey(defaultContent) : null;
14+
let fallbackChunk;
15+
if (typeof defaultChunks !== 'undefined') {
16+
fallbackChunk = useMemo(
17+
() => defaultChunks.find(chunkItem => chunkItem.identifier === identifier),
18+
[defaultChunks, identifier]
19+
);
20+
}
1421
const url = identifier
1522
? `chunks/${identifier}`
1623
: `chunks/${contentKey}?project_id=${projectId}`;
@@ -44,8 +51,6 @@ export function useChunk(defaultContent, { identifier, type }) {
4451
};
4552
}
4653

47-
const fallbackChunk = defaultChunks.filter(chunkItem => chunkItem.identifier === identifier)[0];
48-
4954
if (!chunk) {
5055
if (!defaultContent && !fallbackChunk) {
5156
let cachedChunk;

0 commit comments

Comments
 (0)