2016-12-28 52 views
0

以我非tyepscript限定性反應組分我不得不打字稿在陣營:上`this`

componentWillMount() { 
    this.delayedSearch = _.debounce((val) => { 
     this.onQuerySearch(val); 
    }, 1000); 
} 

用於對輸入字段去抖動打字。然後在輸入欄上我有<input onChange={event => this.delayedSearch(e.value)}>

但是現在,當我切換到Typescipt,我的_.debounce分配給我一個錯誤:

Property 'delayedSearch' does not exist on type 'UserSearch'.

我該如何解決這個問題?

回答

2

這是因爲你的類沒有聲明有這個delayedSearch屬性,所以編譯器不能理解這個屬性是什麼。

在你的類,你應該是這樣的:

class MyComponent extends React.Component<MyProps, MyState> { 
    private delayedSearch: (val: string) => void; 

    componentWillMount() { 
     // should be fine now 
     this.delayedSearch = _.debounce((val) => { 
      this.onQuerySearch(val); 
     }, 1000); 
    } 
}