Skip to content

Commit

Permalink
fix: Fix errors in next.js #130
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed May 12, 2021
1 parent d5d1c80 commit 0ea7b95
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/CustomOverlay/useCustomOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function useCustomOverlay(props = {} as UseCustomOverlay) {
};
}, [customOverlay, map]);
useMemo(() => {
if (map && !portal) {
if (map && !portal && document) {
const elm = document.createElement('div');
const CustomOverlay = getCustomOverlay();
const portalInstance = ReactDOM.createPortal(children, elm);
Expand All @@ -91,6 +91,7 @@ export default function useCustomOverlay(props = {} as UseCustomOverlay) {
setPortal(portalInstance);
setCount(count + 1);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [children, customOverlay]);

useProperties<BMap.Overlay, UseCustomOverlay>(customOverlay!, props, ['ZIndex', 'Offset', 'Position', 'Visiable']);
Expand Down
3 changes: 2 additions & 1 deletion src/PointCollection/usePointCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function usePointCollection(props = {} as UsePointCollection) {
useEffect(() => {
if (!BMap || !map) return;
// 判断当前浏览器是否支持绘制海量点
if (!document.createElement('canvas').getContext) return;
if (document && !document.createElement('canvas').getContext) return;
const opts = { shape, color, size };
if (!pointCollection) {
if (!opts.size) opts.size = BMAP_POINT_SIZE_SMALL;
Expand All @@ -38,6 +38,7 @@ export default function usePointCollection(props = {} as UsePointCollection) {
}
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [map]);

useEffect(() => {
Expand Down
17 changes: 10 additions & 7 deletions src/utils/requireScript.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const headElement = document.head || document.getElementsByTagName('head')[0];
const headElement = (() => {
if (!document) return;
return document.head || document.getElementsByTagName('head')[0];
})();
const _importedScript: { [src: string]: true } = {};

/**
* load dependency by css tag
*/
export function requireCss(src: string): Promise<void> {
return new Promise((resolve, reject) => {
if (src in _importedScript) {
if (src in _importedScript || !document) {
resolve();
return;
}
Expand All @@ -15,14 +18,14 @@ export function requireCss(src: string): Promise<void> {
script.rel = 'stylesheet';
script.href = src;
script.onerror = (err) => {
headElement.removeChild(script);
headElement!.removeChild(script);
reject(new URIError(`The css ${src} is no accessible.`));
};
script.onload = () => {
_importedScript[src] = true;
resolve();
};
headElement.appendChild(script);
headElement!.appendChild(script);
});
}

Expand All @@ -31,7 +34,7 @@ export function requireCss(src: string): Promise<void> {
*/
export function requireScript(src: string): Promise<void> {
return new Promise((resolve, reject) => {
if (src in _importedScript) {
if (src in _importedScript || !document) {
resolve();
return;
}
Expand All @@ -40,13 +43,13 @@ export function requireScript(src: string): Promise<void> {
script.id = '_react_baidu_map';
script.src = src;
script.onerror = (err) => {
headElement.removeChild(script);
headElement!.removeChild(script);
reject(new URIError(`The Script ${src} is no accessible.`));
};
script.onload = () => {
_importedScript[src] = true;
resolve();
};
headElement.appendChild(script);
headElement!.appendChild(script);
});
}

0 comments on commit 0ea7b95

Please sign in to comment.