我是React的新手,我正在嘗試編寫一個使用API的應用程序。我一直收到這個錯誤:TypeError:this.setState不是函數當我嘗試處理API響應時。我懷疑這個綁定有問題,但我無法弄清楚如何修復它。這是我的組件的代碼:var AppMain = React.createClass({ getInitialState: function() { return{ FirstName: " " }; }, componentDidMount:function(){ VK.init(function(){ console.info("API initialisation successful"); VK.api('users.get',{fields: 'photo_50'},function(data){ if(data.response){ this.setState({ //the error happens here FirstName: data.response[0].first_name }); console.info(this.state.FirstName); } }); }, function(){ console.info("API initialisation failed"); }, '5.34'); }, render:function(){ return ( <div className="appMain"> <Header /> </div> ); }});
反應this.setState不是一個函數
MMTTMM
2019-09-20 14:55:17