React为何在使用的时候传入props,这里会多出一层呢?

我创建了一个React组件:

export type TabsWithCrumbPropsType = {
  label: string, 
  breadcrumbs: string[],  
  children: React.ReactNode,
  closable?: boolean
}[]

function TabsWithCrumb(props: TabsWithCrumbPropsType) {
...
}

但是当我在使用的时候传递数据的时候:

import { TabsWithCrumbPropsType } from './components/TabsWithCrumbs'

const defaultProps:TabsWithCrumbPropsType = [{
  label: '001',
  breadcrumbs: ['01', '02', '03'],
  children: <button>001</button>,
  closable: true 
  },
  {
    label: '002',
    breadcrumbs: ['01', '02', '04'],
    children: <button>002</button>
  }
]

<TabsWithCrumbs props={defaultProps}></TabsWithCrumbs>

报错:
image.png

我在组件中打印接受到的参数props,结果为:

image.png

感觉多了几层。

阅读 3k
2 个回答

function TabsWithCrumb(props: TabsWithCrumbPropsType) 改为 function TabsWithCrumb({ props }: { props: TabsWithCrumbPropsType })

定props:

type TabsWithCrumbProps = {
  data: TabsWithCrumbPropsType
};

function TabsWithCrumb(props: TabsWithCrumbProps) {
  // 在组件里用 props.data 
  ...
}

组件里:

function TabsWithCrumb(props: TabsWithCrumbProps) {
  console.log(props.data);  
  ...
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题