javascript - Get react element string type - Stack Overflow

If I have a ponent that renders the following:<div><Example><span>hi<span>&l

If I have a ponent that renders the following:

<div>
  <Example/>
  <span>hi</span>
</div>

The property this.props.children will be an array with two elements. When looped over child.type works only for span to return the string span, what property is there that will give me the string Example for a react element?

If I have a ponent that renders the following:

<div>
  <Example/>
  <span>hi</span>
</div>

The property this.props.children will be an array with two elements. When looped over child.type works only for span to return the string span, what property is there that will give me the string Example for a react element?

Share Improve this question asked Sep 24, 2015 at 4:14 ThomasReggiThomasReggi 59.7k97 gold badges259 silver badges459 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

If the ponent class has a display name, you should be able to use child.type.displayName. This example demonstrates.

Use this to get the string regardless if it's a native DOM Element or ReactElement.

function getStringType (node) {
  if (typeof node.type === 'string') return node.type
  if (node.type && node.type.displayName) return node.type.displayName
  return false
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744245089a4564888.html

相关推荐

  • javascript - Get react element string type - Stack Overflow

    If I have a ponent that renders the following:<div><Example><span>hi<span>&l

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信