remove inline function
Closed this issue · 0 comments
flameddd commented
改寫 inline function 的寫法
react class component 的 function 宣告有正確的方法。
- https://reactjs.org/docs/handling-events.html
- https://cdb.reacttraining.com/react-inline-functions-and-performance-bdff784f5578
不能這樣寫
https://github.com/BensonLiao/line-liff-react/blob/master/src/pages/LIFFWindow.js
只有在很不重要的元件上、加上不重要的產品(如個人測試小東西)等等 才用 inline bind(this)
偷懶
這個是寫 react 的重要觀念,如果你還是沒有很懂,下次我講一遍。
export default class LIFFWindow extends Component {
constructor(props) {
super(props);
this.textInput = null;
this.setTextInputRef = this.setTextInputRef.bind(this)
this.openLIFFWindow = this.openLIFFWindow.bind(this)
this.openLIFFWindowExternal = this.openLIFFWindowExternal.bind(this)
}
setTextInputRef = element => {
this.textInput = element;
};
openLIFFWindow() {
liffHelper.openWindow(this.textInput.value, false);
}
openLIFFWindowExternal() {
liffHelper.openWindow(this.textInput.value, true);
}
closeWindow() {
liffHelper.closeWindow()
}
render() {
return (
// 砍掉其他 DOM 方便你看 code
<input ref={this.setTextInputRef} type="text" className="form-control" id="userid" defaultValue="https://www.google.com" />
<button type="button" class="btn btn-default" onClick={this.openLIFFWindow}>Open Url in LINE Browser</button>
<button type="button" class="btn btn-default" onClick={this.openLIFFWindowExternal}>Open Url in External Browser</button>
<button type="button" className="btn btn-default" onClick={this.closeWindow}>Close LIFF</button>
);
}
}