簡體   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