[英]React Component this.variable not rendering
I have this react component and I have created a const see below: 我有这个react组件,并且创建了一个const,如下所示:
export class MyComponent extends React.Component {
constructor() {
super();
const mytext = 'Some Text';
}
render() {
return (
<div>
{this.mytext}
</div>
);
}
}
This const is not rendering mytext when I use {this.mytext} 当我使用{this.mytext}时,此const不会呈现mytext
What I'm I doing wrong? 我做错了什么?
{this.mytext}
should be {mytext}
{this.mytext}
应该是{mytext}
And if you want to declare a global variable of type const than you need to define it like this 而且,如果您要声明const类型的全局变量,则需要像这样定义它
const mytext = 'Some Text';
export class MyComponent extends React.Component {
constructor() {
super();
}
render() {
return (
<div>
{mytext}
</div>
);
}
EDIT 1. The better approcah to declare a global varibale can be: 编辑1.声明全局变量的更好方法是:
export class MyComponent extends React.Component {
constructor() {
super();
this.mytext = "VED"//you can use it now anywhere inside your file
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.