Как объявить и переназначить переменную в React Class Component для хранения ссылки setTimeout? - PullRequest
0 голосов
/ 26 февраля 2020

Используемый для реагирования на компоненты функции, я обычно объявляю константы внутри функции. Но теперь я должен объявить переменную в компоненте класса и попробовал три разных способа:

  constructor(props: HWTrendChartProps) {
    super(props);
    testVarible = 'this is a test'; // 'testVarible' is not defined.eslint(no-undef)
  }
  constructor(props: HWTrendChartProps) {
    super(props);
    this.testVarible = 'this is a test'; // Property 'testVarible' does not exist on type 'classname'.ts(2339)
  }
private static timeoutRef: any; // works but have to re-assign using classname.timeoutRef = blahblah;

Я пытаюсь сохранить ссылку setTimeout в компоненте класса, поэтому я май clearTimeout от componentWillUnmount. Я не хочу добавлять это, чтобы заявить, могу ли я избежать этого.

1 Ответ

0 голосов
/ 26 февраля 2020

Вам необходимо объявить свойство как поле класса:

class MyClass {
 private testVariable: string
 constructor(props: HWTrendChartProps) {
    super(props);
    this.testVarible = 'this is a test'; // Property 'testVarible' does not exist on type 'classname'.ts(2339)
  }
}
...