鍍金池/ 問答/HTML/ react通過子組件向父組件傳值修改父組件state失敗

react通過子組件向父組件傳值修改父組件state失敗

react通過子組件向父組件props的函數(shù)傳參,傳值,修改父組件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}/>
回答
編輯回答
孤巷

constructor中的

this.handleFetch.bind(this)

改為

this.handleFetch = this.handleFetch.bind(this)
2017年10月22日 20:02
編輯回答
逗婦惱
<NewsList type="search" id={this.state.keyword} handleFetch={this.handleFetch.bind(this)}/>
2017年8月4日 02:17