React child cannot be an object - Storybook
P粉184747536
P粉184747536 2024-03-29 08:43:17
0
1
329

I'm creating a storybook for my design system and I'm getting the following error:

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

If you import the component into another file it works perfectly with the same data, for example:

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

But in Storybook it doesn't work, showing the above error.

I've put the example at this link:

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

P粉184747536
P粉184747536

reply all(1)
P粉904450959

I managed to solve this problem. For some unknown reason, I can't use the word "Child"

in the interface

The problem was solved when I changed from children to items

Before:

import { ReactNode } from "react";

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

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

after

import { ReactNode } from "react";

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

export interface TabsProps {
    type?: "default" | "secondary";
    defaultValue?: string;
    items: Tab[];
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!