Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get string name of React Native component class?

Tags:

react-native

I want print class name with string format in react native programmatically. Can any one let me know that, how can I do this?

like image 809
Akhi Avatar asked Dec 19 '16 06:12

Akhi


People also ask

Can a React component have a className?

React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In class-based components, the className attribute is used to set or return the value of an element's class attribute.

How do you give a component name in React?

You assigned the component in a variable. var TestComponent = React. createClass({ render: function() { return <h1>Hello, {this.props.name}</h1>; } }); Now displayName is set to TestComponent .

How do I give className to component?

To pass class names as props to a React component:Pass a string containing the class names as a prop. Destructure the prop in the child component. Assign the class names to an element, e.g. <h2 className={className}>Content</h2> .


1 Answers

In JavaScript you can get the name of the class by using instance.constructor.name.

class Demo extends React.Component {
    componentDidMount(){
        console.log(this.constructor.name);  // "Demo"
    }
}

For JSX components you can use the instance.type.displayName. For instance:

let text = <Text></Text>;
console.log(text.type.displayName) // "Text"
like image 82
martinarroyo Avatar answered Sep 30 '22 09:09

martinarroyo