Skip to content

Commit

Permalink
fix map function error
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Dec 15, 2023
1 parent c08f64e commit 7078e9e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
6 changes: 5 additions & 1 deletion packages/myreact-dom/src/client/renderDispatch/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ if (__DEV__) {

const re = get();

re.then((res) => (parse(res), res)).then((res) => unmountFiber(res.__fiber__));
re.then((res) => (parse(res), res)).then((res) => {
unmountFiber(res.__fiber__);
res.__container__.isAppMounted = false;
res.__container__.isAppUnmounted = true;
});

return re;
},
Expand Down
12 changes: 11 additions & 1 deletion packages/myreact/src/children/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@ export const map = (
arrayLike: MaybeArrayMyReactElementNode,
action: (child: MyReactElementNode, index: number, children: ArrayMyReactElementNode) => MyReactElementNode
) => {
if (arrayLike === null) return arrayLike;

return mapByJudge(
arrayLike,
(v) => v !== undefined && v !== null,
(child, index, children) => {
const element = action(child, index, children);
return cloneElement(element, { key: typeof element === "object" ? (typeof element?.key === "string" ? `${element.key}` : `.${index}`) : null });
if (isValidElement(element)) {
return cloneElement(element, { key: typeof element === "object" ? (typeof element?.key === "string" ? `${element.key}` : `.${index}`) : null });
} else {
return element;
}
}
);
};

export const toArray = (arrayLike: MaybeArrayMyReactElementNode) => {
if (arrayLike === null) return [];

return mapByJudge(
arrayLike,
(v) => v !== undefined && v !== null,
Expand All @@ -31,6 +39,8 @@ export const forEach = (
arrayLike: MaybeArrayMyReactElementNode,
action: (child: MyReactElement, index: number, children: ArrayMyReactElementNode) => MyReactElement
) => {
if (arrayLike === null) return arrayLike;

mapByJudge(arrayLike, (v) => v !== undefined && v !== null, action);
};

Expand Down
33 changes: 19 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ui/ssr-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"pino-pretty": "^10.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-grid-layout": "^1.3.4",
"react-grid-layout": "^1.4.4",
"react-helmet-async": "^1.3.0",
"react-icons": "^4.10.1",
"react-intl": "^6.4.4",
Expand Down Expand Up @@ -82,7 +82,7 @@
"@types/node": "^20.5.7",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"@types/react-grid-layout": "^1.3.2",
"@types/react-grid-layout": "^1.3.5",
"@types/react-resizable": "^3.0.4",
"@types/webpack-env": "^1.18.1",
"cross-env": "^7.0.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const targetRender: SafeAction = async ({ req, res, store, lang, env, ass
onShellError(err) {
error = true;
if (!initial) {
initial = true;
if (!env.isSTATIC) {
// Something errored before we could complete the shell so we fallback to client render
targetCSRRender({ req, res, store, lang, env, assets });
Expand All @@ -78,6 +79,7 @@ export const targetRender: SafeAction = async ({ req, res, store, lang, env, ass
onError(err) {
error = true;
if (!initial) {
initial = true;
if (!env.isSTATIC) {
// not set header, so we can safe to fallback to client render
targetCSRRender({ req, res, store, lang, env, assets });
Expand Down

0 comments on commit 7078e9e

Please sign in to comment.