
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 23, 2022 05:25 AM
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:
function OrderLineItems(props) {
return (
<section className={this.constructor.name}>
</section>
)
}
Does anyone have a solution for this?
Solved! Go to Solution.
Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2022 02:17 AM
@Jack_Manuel +1
The following works
function OrderLineItems(props) {
return (
<section className={OrderLineItems.name}>
</section>
)
}
Not necessarily ideal as you might have been trying to avoid referencing the function name.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 23, 2022 06:22 AM
I believe the {this.constructor.name} method would only work with a class component and you have a functional component there.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2022 02:17 AM
@Jack_Manuel +1
The following works
function OrderLineItems(props) {
return (
<section className={OrderLineItems.name}>
</section>
)
}
Not necessarily ideal as you might have been trying to avoid referencing the function name.
