react通过子组件向父组件传值修改父组件state失败

react通过子组件向父组件props的函数传参,传值,修改父组件state,父组件报错

this.setState is not a function

子组件:

fetch(`${host}/web/news/search_in_channel_with_pub_info?channelId=${channelId}&q=${id}&page=${_this.state.page}&size=${_this.state.size}`,{
            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}/>
阅读 3.7k
2 个回答

constructor中的

this.handleFetch.bind(this)

改为

this.handleFetch = this.handleFetch.bind(this)
<NewsList type="search" id={this.state.keyword} handleFetch={this.handleFetch.bind(this)}/>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题