React child tidak boleh menjadi objek - Buku cerita
P粉184747536
P粉184747536 2024-03-29 08:43:17
0
1
369

Saya sedang mencipta buku cerita untuk sistem reka bentuk saya dan saya mendapat ralat berikut:

Error: Objects are not valid as a React child (found: object with keys {icon, label, content}). If you meant to render a collection of children, use an array instead.
  at mapIntoArray (/node_modules/.cache/.vite-storybook/deps/chunk-LWCIAKPC.js?v=909d2fcb:701:31))
  at mapChildren (/node_modules/.cache/.vite-storybook/deps/chunk-LWCIAKPC.js?v=909d2fcb:736:11))
  at Object.toArray (/node_modules/.cache/.vite-storybook/deps/chunk-LWCIAKPC.js?v=909d2fcb:754:18))
  at parseReactElement2 (/node_modules/.cache/.vite-storybook/deps/@storybook_react_preview.js?v=909d2fcb:512:49))
  at reactElementToJsxString2 (/node_modules/.cache/.vite-storybook/deps/@storybook_react_preview.js?v=909d2fcb:848:21))
  at /node_modules/.cache/.vite-storybook/deps/@storybook_react_preview.js?v=909d2fcb:1437:173
  at /node_modules/.cache/.vite-storybook/deps/chunk-LWCIAKPC.js?v=909d2fcb:737:25
  at mapIntoArray (/node_modules/.cache/.vite-storybook/deps/chunk-LWCIAKPC.js?v=909d2fcb:660:31))
  at Object.mapChildren [as map] (/node_modules/.cache/.vite-storybook/deps/chunk-LWCIAKPC.js?v=909d2fcb:736:11))

Tabs.stories.tsx

import { Tabs } from './index';
import { Meta, StoryObj } from "@storybook/react"
import { Gear, PaintBrush, PencilSimple } from "phosphor-react";

const items = [
    {
        icon: <PencilSimple size={20} />,
        label: 'Item 1',
        children: <div>Item 1 content</div>
    },
    {
        icon: <Gear size={20} />,
        label: 'Item 2',
        children: <div>Item 2 content</div>
    },
    {
        icon: <PaintBrush size={20} />,
        label: 'Item 3',
        children: <div>Item 3 content</div>
    },

];

export default {
    title: 'Components/Tabs',
    component: Tabs,
    args: {
        children: items,
        type: "default"
    },
} as Meta

export const Default:StoryObj = {

}

TabsProps.types.ts

import { ReactNode } from "react";

interface Tab {
    label?: string;
    icon?: ReactNode,
    content: ReactNode
}

export interface TabsProps {
    type?: "default" | "secondary";
    defaultValue?: string;
    children: Tab[];
}

Ia berfungsi dengan sempurna dengan data yang sama jika anda mengimport komponen ke dalam fail lain, contohnya:

<Tabs defaultValue={'tab-0'} children={items} />

Tetapi dalam Buku Cerita ia tidak berfungsi, menunjukkan ralat di atas.

Saya telah meletakkan contoh di pautan ini:

https://codesandbox.io/s/naughty-monad-ql4h2e?file=/src/App.tsx

P粉184747536
P粉184747536

membalas semua(1)
P粉904450959

Saya berjaya menyelesaikan masalah ini. Atas sebab yang tidak diketahui saya tidak boleh menggunakan perkataan "Child" dalam antara muka

Masalah itu selesai apabila saya menukar daripada kanak-kanak kepada item

Sebelum:

import { ReactNode } from "react";

interface Tab {
    label?: string;
    icon?: ReactNode,
    content: ReactNode
}

export interface TabsProps {
    type?: "default" | "secondary";
    defaultValue?: string;
    children: Tab[];
}

Selepas

import { ReactNode } from "react";

interface Tab {
    label?: string;
    icon?: ReactNode,
    content: ReactNode
}

export interface TabsProps {
    type?: "default" | "secondary";
    defaultValue?: string;
    items: Tab[];
}
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan