好像有很多這樣的錯誤的方法和我相當肯定,我想這樣做的錯誤的方式(請注意這個代碼不目前的工作):在React + Redux中將參數傳遞給reducer的正確方法?
class SubmitLink extends React.Component<SubmitLinkProps, {}>{
constructor(props: SubmitLinkProps) {
super(props);
this.urlToPass = "nothing";
}
urlToPass: string;
handleChange(e: React.FormEvent<HTMLInputElement>) {
this.urlToPass = e.currentTarget.value;
}
public render() {
return <div>
<div>hello world {this.props.url}</div>
<input onChange={this.handleChange} type='text'></input>
<button onClick={() => {
this.props.submitlink(this.urlToPass);
}}>submit</button>
</div>
}
}
除了事實代碼不起作用(urlToPass在運行時未定義,不確定原因)我只是爲了從文本字段獲取輸入而看起來像一大堆工作。同時,這是我可以找到如何做到這一點的唯一方法,但它確實感覺不對。
另外值得一提的是,使用ES6語法自動綁定「這個」到功能:myFunction的= (params)=> {code}; – Adam
好叫出亞當! – TheBottleSeller
嗯試着這個代碼我得到這個錯誤:TS2339:'只讀<{}>'類型不存在屬性'urlToPass'。 – tweetypi