有人可以向我解释为什么在函数“calcTime”中的下例中的“this.state.time”在“componentWillReceiveProps”后没有更新?componentWillReceiveProps不更新函数中的状态
这有点奇怪,因为每当组件接收新道具时,“文本”字段中的this.state.time会更新,但在函数“calcTime”中,“this.state.time”始终保持从“this .props.time”。
谢谢。
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
export default class Time extends Component {
constructor(props){
super(props);
this.state = {
time:this.props.Time,
info:''
};
}
calcTime(){
console.log('in calcTime '+ this.state.time)
}
componentWillReceiveProps(nextProps) {
this.setState({
time:nextProps.Time
});
this.calcTime();
}
render(){
return(
<View>
<Text>{this.state.time}</Text>
</View>
);
}
}
AppRegistry.registerComponent('Time',() => Time);
([在reactJs初始呼叫空状态值]的可能的复制http://stackoverflow.com/questions/39567602/empty-state-value-for-initial- call-in-reactjs) –