父组件 BECanlendar
子组件 Canlendar
class BECanlendar extends React.Component{
constructor(){
super();
this.state ={
"year":2005,
"month":1,
"day":5
}
}
onpick({year , month , day}){
console.log("我是BE中的onpick");
this.setState({year , month , day});
}
render(){
<div className="end_result result">
//通过onpick={(this.onpick).bind(this)} 绑定函数从子组件传回父组件
<Canlendar {...this.state} onpick={(this.onpick).bind(this)} />
</div>
}
}
class Canlendar extends React.Component{
goNextMonth(){
if(this.state.month != 12){
//这种setState只能刷新当前页面,并不能传回父组件
this.setState({"month" : this.state.month + 1});
}
}
componentDidMount(){
var self = this;
$(this.refs.table).delegate("td.inmonth","click",function(){
self.setState({"day" : Number($(this).attr("data-day"))})
//传给父亲,通过this.props.onpick(),this.props.传入属性,来传回至父组件使其该百年
self.props.onpick(self.state);
});
}