2017-04-17 24 views
0

をレンダリングしないthis.variableと私は以下を参照のconstを作成しました:私が使用している場合リアクトコンポーネント私はこのコンポーネントを反応させてきた

export class MyComponent extends React.Component { 
    constructor() { 
    super(); 
    const mytext = 'Some Text'; 
    } 

    render() { 
    return (
     <div> 
     {this.mytext} 
     </div> 

    ); 
    } 
} 

はこのconstのはMYTEXTをレンダリングされていない{this.mytext}

何私は間違っている?

+1

'const this.mytext = 'Some text';' {mytext} '......! – Jai

+0

mytextのデータ型はconstです。だからあなたはそれを{this.mytext}から{mytext}に変更する必要があります – Ved

+0

@Jai私はconst this.mytext = 'Some Text'と思います。構文が間違っています。 – Ved

答えて

2

{this.mytext}{mytext}

する必要があり、あなたがタイプconstののグローバル変数を宣言したい場合は、宣言するためのより良いapprocah 1.この

const mytext = 'Some Text'; 
    export class MyComponent extends React.Component { 
     constructor() { 
     super(); 

     } 

    render() { 
     return (
      <div> 
      {mytext} 
      </div> 

     ); 
     } 

EDITのように、それを定義する必要がありますよりも、グローバル変数は次のようになります。

export class MyComponent extends React.Component { 
     constructor() { 
     super(); 
     this.mytext = "VED"//you can use it now anywhere inside your file 
     } 
+0

コンポーネント内に変数を定義することはできませんか? – JakeBrown777

+0

いいえthis.mytextのように定義できますが、const mytextはできません。 – Ved

関連する問題