import React from 'react'; const STR = '被調用,this指向:'; class App extends React.Component{ constructor(){ super() } //測試函數 handler() { console.log(`handler ${STR}`,this); } render(){ console.log(`render ${STR}`,this); return( <div> <h1>hello World</h1> <label htmlFor = 'btn'>單擊打印函數handler中this的指向</label> <input id = "btn" type="button" value = '單擊' onClick = {this.handler}/> </div> ) } } export default App
結果如圖:
html
var student = { func: function() { console.log(this); }; }; student.func(); var studentFunc = student.func; studentFunc();
這段代碼運行,能夠看到student.func()打印了student對象,由於此時this指向student對象;而studentFunc()打印了window,由於此時由window調用的,this指向window。react
這段代碼形象的驗證了,JavaScript函數中的this不是在函數聲明的時候,而是在函數運行的時候定義的;app
一樣,React組件也遵循JavaScript的這種特性,因此組件方法的‘調用者’不一樣會致使this的不一樣(這裏的 「調用者」 指的是函數執行時的當前對象)dom
/App.jsx
import React from 'react'; const STR = '被調用,this指向:'; class App extends React.Component{ constructor(){ super() } ComponentDidMount() { console.log(`ComponentDidMount ${STR}`,this); } componentWillReceiveProps() { console.log(`componentWillReceiveProps ${STR}`,this); } shouldComponentUpdate() { console.log(`shouldComponentUpdate ${STR}`,this); return true; } componentDidUpdate() { console.log(`componentDidUpdate ${STR}`,this); } componentWillUnmount() { console.log(`componentWillUnmount ${STR}`,this); } //測試函數 handler() { console.log(`handler ${STR}`,this); } render(){ console.log(`render ${STR}`,this); this.handler(); window.handler = this.handler; window.handler(); return( <div> <h1>hello World</h1> <label htmlFor = 'btn'>單擊打印函數handler中this的指向</label> <input id = "btn" type="button" value = '單擊' onClick = {this.handler}/> </div> ) } } export default App
能夠看到:函數
/index.js
import React from 'react' import {render,unmountComponentAtNode} from 'react-dom' import App from './App.jsx' const root=document.getElementById('root') console.log("首次掛載"); let instance = render(<App />,root); window.renderComponent = () => { console.log("掛載"); instance = render(<App />,root); } window.setState = () => { console.log("更新"); instance.setState({foo: 'bar'}); } window.unmountComponentAtNode = () => { console.log('卸載'); unmountComponentAtNode(root); }
使用三個按鈕觸發組件的裝載、更新和卸載過程:測試
/index.html
<!DOCTYPE html> <html> <head> <title>react-this</title> </head> <body> <button onclick="window.renderComponent()">掛載</button> <button onclick="window.setState()">更新</button> <button onclick="window.unmountComponentAtNode()">卸載</button> <div id="root"> <!-- app --> </div> </body> </html>
1. render()以及componentDIdMount()、componentDIdUpdate()等其餘生命週期函數中的this都是組件實例; 2. this.handler()的調用者,爲render()中的this,因此打印組件實例; 3. window.handler()的「調用者」,爲window,因此打印window; 4. onClick={this.handler}的「調用者」爲事件綁定,來源多樣,這裏打印undefined。 - 面對如此混亂的場景,若是咱們想在onClick中調用自定義的組件方法,並在該方法中獲取組將實例,咱們就得進行轉換上下文即綁定上下文:
import React from 'react'; const STR = '被調用,this指向:'; class App extends React.Component{ constructor(){ super(); this.handler = this.handler.bind(this); } //測試函數 handler() { console.log(`handler ${STR}`,this); } render(){ console.log(`render ${STR}`,this); this.handler(); window.handler = this.handler; window.handler(); return( <div> <h1>hello World</h1> <label htmlFor = 'btn'>單擊打印函數handler中this的指向</label> <input id = "btn" type="button" value = '單擊' onClick = {this.handler}/> </div> ) } } export default App