react通过子组件向父组件props的函数传参,传值,修改父组件state,父组件报错
this.setState is not a function
子组件:
fetch(``,{
method:'GET',
mode:'cors',
}).then(function(response){
_this.setState({
loading:false
})
return response.json().then(function(res){
if(res.content.length===0){
_this.props.handleFetch("false"); //在这里给父组件传值
}
_this.setState({
newsList:res.content,
totalPages:res.totalPages
});
});
父组件:
constructor(props){
super(props);
this.state = {
keyword:this.props.match.params.id,
result:"true",
_isMounted:true
};
this.handleFetch.bind(this)
}
handleFetch(status){
console.log(status) //可以打印 出传来的false
this.setState({
result:status
})
}
<NewsList type="search" id={this.state.keyword} handleFetch={this.handleFetch}/>
心有法竹
喵喵时光机