繁体   English   中英

从React中的Axios返回获取的数据

[英]Return Fetched Data from Axios in React

有关于使用Axios渲染获取的数据的问题。 我可以将返回的数据记录到控制台,但是不会在屏幕上呈现。

我正在使用NPM Bitly模块: https ://www.npmjs.com/package/bitly

const BitlyClient = require('bitly');
const bitly = BitlyClient('ACCESS TOKEN');

  class Example extends Component {
    constructor() {
        super();
        this.state = { 
        landing: 'https://www.google.com/',
        newUrl: 'https://www.udacity.com/'
};

API调用

 componentDidMount() {
      bitly.shorten(this.state.landing)
      .then((response) => {
        this.setState({newUrl: response.data.url })
        console.log(response.data.url);
      })
      .catch(function(error) {
        console.error(error);
      });
    }

这会将数据返回到控制台,但不会呈现到页面。

渲染到页面

<Component> {this.newUrl} </>

我想念什么?

它应该是{this.state.newUrl}

从字面上看,我刚发布此邮件开始工作。

我刚刚更新了组件以包括状态。

不工作

<Component> {this.newUrl} </>

有用吗

<Component> {this.state.newUrl} </>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM