Get component name in React

前端 未结 5 1930
暗喜
暗喜 2020-12-05 18:08

I\'m developing a React application. I have a Loading component, which is a little animation for waiting. I want to add a message in this Loading component according to the

5条回答
  •  一向
    一向 (楼主)
    2020-12-05 18:51

    You don't need it to be dynamic since you are writing the component yourself and can pass it as a string

    class LoginForm extends React.Component {
        render() {   
            return (
                
    {this.state.displayLoading && }
    ); } }

    However if you still need to access the name of the component, you could define the displayName property on the component

    class LoginForm extends React.Component {
       static displayName = 'LoginForm';
        render() {   
            return (
                
    {this.state.displayLoading && }
    ); } }

    and access it like Component.displayName.

提交回复
热议问题