Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确地将代码从angularjs迁移到reactjs_Angularjs_Reactjs - Fatal编程技术网

如何正确地将代码从angularjs迁移到reactjs

如何正确地将代码从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

我正在尝试将代码从angular迁移到react。不确定是否正确,只是需要一些帮助,如果我在正确的方向与否。我不知道,所以我很困惑“textdata”是否类似于react中的状态,我是否必须在顶部的状态中声明它

角码

 $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。