From b118f23de2046992f06a7f2b67a249920235763f Mon Sep 17 00:00:00 2001 From: Xaber Date: Wed, 7 Jun 2017 23:22:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这里不是因为需要绑定this,而是因为在 setInterval 时候,this.tick 被作为了一个单纯的函数。 见: var a = { name: 'Xaber', say() { alert(this.name) } }; setTimeout(a.say, 1000); --- Ch04/props-state-introduction.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Ch04/props-state-introduction.md b/Ch04/props-state-introduction.md index 551b453..be3b2a0 100644 --- a/Ch04/props-state-introduction.md +++ b/Ch04/props-state-introduction.md @@ -112,8 +112,6 @@ app.js: class Timer extends React.Component { constructor(props) { super(props); - // 與 ES5 React.createClass({}) 不同的是 component 內自定義的方法需要自行綁定 this context,或是使用 arrow function - this.tick = this.tick.bind(this); // 初始 state,等於 ES5 中的 getInitialState this.state = { secondsElapsed: 0, @@ -125,7 +123,7 @@ class Timer extends React.Component { } // componentDidMount 為 component 生命週期中階段 component 已插入節點的階段,通常一些非同步操作都會放置在這個階段。這便是每1秒鐘會去呼叫 tick 方法 componentDidMount() { - this.interval = setInterval(this.tick, 1000); + this.interval = setInterval(this.tick.bind(this), 1000); } // componentWillUnmount 為 component 生命週期中 component 即將移出插入的節點的階段。這邊移除了 setInterval 效力 componentWillUnmount() {