简体   繁体   English

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

[英]Return Fetched Data from Axios in React

Have a question about rendering fetched data with Axios. 有关于使用Axios渲染获取的数据的问题。 I'm able to log returned data to the console, however, it will not render on the screen. 我可以将返回的数据记录到控制台,但是不会在屏幕上呈现。

I'm using an NPM Bitly module: https://www.npmjs.com/package/bitly 我正在使用NPM Bitly模块: https ://www.npmjs.com/package/bitly

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

State

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

API Call 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);
      });
    }

This returns data to the console but does not render to the page. 这会将数据返回到控制台,但不会呈现到页面。

Render to Page 渲染到页面

<Component> {this.newUrl} </>

What am I missing? 我想念什么?

它应该是{this.state.newUrl}

Literally started to work as soon as I posted this smh. 从字面上看,我刚发布此邮件开始工作。

I just updated the component to include the state. 我刚刚更新了组件以包括状态。

Did not work 不工作

<Component> {this.newUrl} </>

Does Work 有用吗

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

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

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