搜尋

首頁  >  問答  >  主體

React child 不能是物件 - Storybook

我正在為我的設計系統建立故事書,但遇到以下錯誤:

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[];
}

如果您將元件匯入另一個檔案中,它可以完美地處理相同的數據,例如:

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

但在 Storybook 中它不起作用,顯示上述錯誤。

我已將範例放在此連結中:

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

##
P粉184747536P粉184747536238 天前340

全部回覆(1)我來回復

  • P粉904450959

    P粉9044509592024-03-30 00:02:17

    我設法解決了這個問題。由於某種未知的原因,我無法在介面中使用“孩子”一詞

    當我從children改為items時,問題就解決了

    之前:

    import { ReactNode } from "react";
    
    interface Tab {
        label?: string;
        icon?: ReactNode,
        content: ReactNode
    }
    
    export interface TabsProps {
        type?: "default" | "secondary";
        defaultValue?: string;
        children: Tab[];
    }

    之後

    import { ReactNode } from "react";
    
    interface Tab {
        label?: string;
        icon?: ReactNode,
        content: ReactNode
    }
    
    export interface TabsProps {
        type?: "default" | "secondary";
        defaultValue?: string;
        items: Tab[];
    }

    回覆
    0
  • 取消回覆