如何正确地将代码从angularjs迁移到reactjs
我正在尝试将代码从angular迁移到react。不确定是否正确,只是需要一些帮助,如果我在正确的方向与否。我不知道,所以我很困惑“textdata”是否类似于react中的状态,我是否必须在顶部的状态中声明它 角码如何正确地将代码从angularjs迁移到reactjs,angularjs,reactjs,Angularjs,Reactjs,我正在尝试将代码从angular迁移到react。不确定是否正确,只是需要一些帮助,如果我在正确的方向与否。我不知道,所以我很困惑“textdata”是否类似于react中的状态,我是否必须在顶部的状态中声明它 角码 $scope.textanalysis=function(){ return $http.post('/api/analyse',{'snippetdesc': snippetDescription}).then(function(response){ if(re
$scope.textanalysis=function(){
return $http.post('/api/analyse',{'snippetdesc': snippetDescription}).then(function(response){
if(response.status==200){
textdata=response.data
textlen=snippetDescription.split(' ').length
}else{
console.log('danger','An error has occured while updating the snippet. Please try again');
}
})
}
我翻译成反应的那个
componentDidMount() {
textanalysis(){
fetch('/api/analyse', {
method: 'POST',
body: JSON.stringify({
snippetdesc: 'snippetDescription'
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
}).then(response => {
return response.json()
}).then(textdata => {
this.setState({
textdata = response.data
textlen=snippetDescription.split(' ').length
});
});
}
试试这个,希望它能起作用 类MyComponent扩展了React.Component{ 建造师(道具){ 超级(道具); 此.state={ text数据:[], textlen:0 }; } textanalysis(){ 获取(“/api/analysis”{ 方法:“POST”, 正文:JSON.stringify({ snippetDescription:“snippetDescription” }), 标题:{ “内容类型”:“应用程序/json;字符集=UTF-8” } }) .then(response=>response.json()) .然后((文本数据)=>{ 这是我的国家({ textdata:textdata.data, textlen:snippetDescription.split(“”).length }); },(错误)=>{ console.log(错误) }) } }
试试这个,希望它能起作用 类MyComponent扩展了React.Component{ 建造师(道具){ 超级(道具); 此.state={ text数据:[], textlen:0 }; } textanalysis(){ 获取(“/api/analysis”{ 方法:“POST”, 正文:JSON.stringify({ snippetDescription:“snippetDescription” }), 标题:{ “内容类型”:“应用程序/json;字符集=UTF-8” } }) .then(response=>response.json()) .然后((文本数据)=>{ 这是我的国家({ textdata:textdata.data, textlen:snippetDescription.split(“”).length }); },(错误)=>{ console.log(错误) }) } }
为什么是textdata.data,不应该是response.data吗?textdata您在里面表示为response,所以响应中的任何数据都将出现在textdata中。在您的代码中,您编写了response.data,但您在其中提到了textdata,因此它将成为textdata.data。为什么它是textdata.data,不应该是response.data?您在其中表示为response的textdata,因此响应中的任何数据都将出现在textdata中。在您的代码中,您已经编写了response.data,但您在其中提到了textdata,因此它将成为textdata.data。