I want print class name with string format in react native programmatically. Can any one let me know that, how can I do this?
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.
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 .
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> .
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With