2016-02-20 29 views
0

我有一个我正在创建的React组件。但this,我,componentDidMounrender里面的价值似乎是不同的。React - 这个内部组件的值DidMount

var App = React.createClass({ 

    getInitialState:() => { 
     console.log('Inside getInitialState') 
     console.log(this) 
     return { 
      title: 'Heading' 
      , content: 'Text' 

     } 
    } 
    , componentDidMount:() => { 
     console.log('Inside componentDidMount') 
     console.log(this) 
    } 
    , render: function() { 
     console.log('Inside render') 
     console.log(this) 
     return <div> 
      <Intro /> 
      <SectionText heading = {this.state.title} text = {this.state.content}/> 
     </div> 
    } 
    , getArticle: id => { 
     articleStore.getArticle(id) 
    } 
    , handleChange:() => { 
     this.setState(articleStore.getState()) 
    } 
}) 

console log

我可以看到预期的道具,状态函数中呈现的this而不是在其他两种方法的情况下。 这是预期的吗?我如何访问我的反应组件的属性,状态,功能?

+3

你知道,当你使用箭头功能的'this'值是不同的this? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions你为什么在一个中使用箭头而不是另一个? – azium

+0

谢谢..愚蠢的我:D – Eels

+0

你可以添加作为答案,如果你想,我会接受它 – Eels

回答