Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unique key error #946

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions packages/react/block.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { createElement, Fragment, useCallback, useMemo, useRef } from 'react';
import type { ComponentType, Ref } from 'react';
import { Fragment, createElement, useCallback, useMemo, useRef } from 'react';
import {
block as createBlock,
mount$,
patch as patchBlock,
} from '../million/block';
import { MapSet$, MapHas$ } from '../million/constants';
import type { Options, MillionProps, MillionPortal } from '../types';
import { MapHas$, MapSet$ } from '../million/constants';
import type { MillionPortal, MillionProps, Options } from '../types';
import { Effect, REGISTRY, RENDER_SCOPE, SVG_RENDER_SCOPE } from './constants';
import { processProps, unwrap } from './utils';
import { Effect, RENDER_SCOPE, REGISTRY, SVG_RENDER_SCOPE } from './constants';

export const block = <P extends MillionProps>(
fn: ComponentType<P> | null,
Expand Down Expand Up @@ -65,17 +65,14 @@ export const block = <P extends MillionProps>(

const childrenSize = portalRef.current.length;
const children = new Array(childrenSize);

children[0] = marker;
children[1] = createElement(Effect, {
effect,
deps: hmrTimestamp ? [hmrTimestamp] : [],
});
for (let i = 0; i < childrenSize; ++i) {
children[i + 2] = portalRef.current[i]?.portal;
children[i] = portalRef.current[i]?.portal;
}

const vnode = createElement(Fragment, { children });
const vnode = createElement(Fragment, {}, marker, createElement(Effect, {
effect,
deps: hmrTimestamp ? [hmrTimestamp] : [],
}), children);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible that portals (children) won't have a key and the warning would be shown again?

What I'd suggest is that you spread the children by , ...children, so this way the whole error would be removed.

}), ...children);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spreading might hit the arguments bound because it has a lower bound than arrays.

return vnode;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/react/compiled-block.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// The following code are for the compield `block` components
import type { ReactPortal, ComponentType, JSX } from 'react';
import { createElement, useState, Fragment } from 'react';
import type { ComponentType, JSX, ReactPortal } from 'react';
import { Fragment, createElement, useState } from 'react';
import type { MillionPortal, MillionProps, Options } from '../types';
import { block } from "./block";
import { renderReactScope } from './utils';
Expand Down
Loading