Help

Getting the component name

Topic Labels: Custom Extensions
Solved
Jump to Solution
1542 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Tom_Keysers
6 - Interface Innovator
6 - Interface Innovator

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?

1 Solution

Accepted Solutions
goksan
6 - Interface Innovator
6 - Interface Innovator

@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.

See Solution in Thread

2 Replies 2
Jack_Manuel
7 - App Architect
7 - App Architect

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

goksan
6 - Interface Innovator
6 - Interface Innovator

@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.