ReactJS - React.Children.forEach - Can I get the child component name?

前端 未结 3 1435
旧巷少年郎
旧巷少年郎 2020-12-29 09:42

I have a React (15.5.4) component with many children, some of which are HTML elements and some are other React components.

I\'m using server rendering and need the s

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 09:55

    If you're using Babel, you can use this Babel plugin to automatically set the displayName, so that child.type.displayName will equal the string of whatever you've named the component:

    https://www.npmjs.com/package/babel-plugin-add-react-displayname

    It's easy to install and use, just read the directions and make sure to add the plugin name add-react-displayname to your plugins array in your .babelrc file.

提交回复
热议问题