componentDidMount() {
$.ajax({
type: "GET",
url: "/tweets",
dataType: "json",
success: function (data){
//$('#tweetsList').append(data);
this.setState({tweetsList: data});
},
error: function (error) {
console.log(error);
}
});
}
上面的代码块给了我一个 this.setState 函数未找到错误。我正在尝试将 json 从我的 Tweets Controller 渲染到我的 main.jsx 文件。
最佳答案
this
的上下文在 ajax 调用中发生变化。 this
不再引用您的组件。您需要引用原始上下文。
componentDidMount() {
const self = this;
$.ajax({
type: "GET",
url: "/tweets",
dataType: "json",
success: function (data){
//$('#tweetsList').append(data);
self.setState({tweetsList: data});
},
error: function (error) {
console.log(error);
}
});
}
关于javascript - this.setState 不是 Rails 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45665158/