renderChildren问题

来源:6-6 日趋完美 - Menu 组件编码第三部分

小白413

2021-02-18

请教一下老师,menuItem.displayName 算是自定义的一个名称吗?
为什么会在 childElement.type 中拿到这个displayName

写回答

1回答

张轩

2021-02-19

同学你好 displayName 是 React.FC 上面的一个标准属性 不是自定义的名称。你看一下 FC 的定义文件就可以发现

interface FunctionComponent<P = {}> {
    (props: PropsWithChildren<P>, context?: any): ReactElement | null;
    propTypes?: WeakValidationMap<P>;
    contextTypes?: ValidationMap<any>;
    defaultProps?: Partial<P>;
    displayName?: string;
}

第二个问题 既然是标准属性 这个属性就会被赋值到 ReactElement 上面,这是 React 内部完成的逻辑。

1
0

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2124 学习 · 959 问题

查看课程