2017-08-04 33 views

回答

0

您将无法在其自己的声明中使用this.state。您应该设置componentDidMount函数的状态,如

this.state = { 
    t: 5 , 
    a:null 
    } 
... 
handleClick =() => { 
    var a = this.give(this.state.t); 
    this.setState({a}); 
} 

give(B){ 
... 

} 

<button onClick={this.handleClick}>Click</button> 
+0

非常感谢你,il是最好的解决方案。 –

+0

但我有一个问题,我不应该使用componentDidMount :(。 –

+0

为什么你不能使用它 –

0

我认为,我们不能将一个变量的状态里面,你可以做一两件事, 最初是在构造函数变量赋值t。此后在构造函数的末尾调用this.give()函数。

constructor(){ 
super() 
this.state = {t:5} 

this.give = this.give.bind(this) 
this.give(this.state.t) 
} 

give(a){ 
youroperation with a 
} 
+0

谢谢朋友的回答。 –

+0

你确定吗?任何方式来分配一个状态内的变量? –