如何正确地将代码从angularjs迁移到reactjs [英] How to correctly migrate the code from angularjs to reactjs

查看:36
本文介绍了如何正确地将代码从angularjs迁移到reactjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将代码从有角度的迁移到反应.不知道它是否正确,如果我朝着正确的方向前进,只需要一些帮助.我不知道角度,所以如果'textdata'是类似React中的状态,我会感到困惑,我是否必须在顶部的状态中声明它

I'm trying to migrate the code from angular to react. Not sure if it's correct, just need some help if I'm going in the right direction or not. I don't know angular so I'm having confusion if the 'textdata' is something like a state in react and will I have to declare it in states at the top or not

角度代码

 $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
      });
    });
}

推荐答案

尝试一下,希望它能起作用.

Try this, Hope it will work.

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      textdata: [],
      textlen: 0
    };
  }

textanalysis(){  
fetch('/api/analyse', {
    method: 'POST',
    body: JSON.stringify({
      snippetdesc: 'snippetDescription'
    }),
    headers: {
      "Content-type": "application/json; charset=UTF-8"
    }
  })
  .then(response =>  response.json())
  .then((textdata) => {
      this.setState({
        textdata : textdata.data,
        textlen : snippetDescription.split(' ').length
      });
    },(error) => {
          console.log(error)
    })
}  
}

这篇关于如何正确地将代码从angularjs迁移到reactjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆