Skip to main content
Solved

Getting the component name


Forum|alt.badge.img+14

Hi, I’m trying to get a component’s name and insert it as a class name. A google search tells me I should use this.constructor.name, but that doesn’t seem to work here.

Eg:

1function OrderLineItems(props) {
2 return (
3
4
5 )
6}
7

Does anyone have a solution for this?

Best answer by goksan

@Jack_Manuel +1

The following works

1function OrderLineItems(props) {
2 return (
3
4
5 )
6}
7

Not necessarily ideal as you might have been trying to avoid referencing the function name.

View original
Did this topic help you find an answer to your question?

2 replies

Jack_Manuel
Forum|alt.badge.img+18
  • Inspiring
  • 79 replies
  • November 23, 2022

I believe the {this.constructor.name} method would only work with a class component and you have a functional component there.


Forum|alt.badge.img+6
  • Inspiring
  • 23 replies
  • Answer
  • November 24, 2022

@Jack_Manuel +1

The following works

1function OrderLineItems(props) {
2 return (
3
4
5 )
6}
7

Not necessarily ideal as you might have been trying to avoid referencing the function name.


Reply