2017-04-30 104 views
1

我一直拉我的頭髮太長,我不能再集中。React.js渲染json響應,coetch或axios

我想從網址中取出json,並在瀏覽器中直觀地呈現它。它甚至不需要格式化,至少在我通過這個障礙之前它不會。

我可以通過console.log將它顯示在控制檯中,但我似乎無法將響應放入render方法中。我已經簡化到下面的代碼,直到我可以看到頁面上的東西。

import React, { Component } from 'react'; 
// import axios from 'axios'; 

var co = require('co'); 
co(function *() { 
var res = yield fetch('https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow'); 
var json = yield res.json(); 
console.log(res); 
}); 

class App extends Component { 

render() { 
return (
    <div className="App"> 
    INSERT JSON HERE 
    </div> 
); 
} 
} 

export default App; 

我也檢索使用

fetch('https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow') 
    .then(function(res) { 
     return res.json(); 
    }).then(function(json) { 
     console.log(json); 
    }); 

我原來用愛可信,因爲我想:「哦,我要去,因爲誰的真棒?我真棒使用Axios公司開始響應。 「

axios.get('https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow') 
    .then(function(response) { 
    console.log(response.data); 
    }); 

但這是一個謬誤,因爲今天我不是很棒。

我會盡我所能的幫助!我原來的計劃還包括使用地圖來迭代「項目」,所以額外的點,如果你可以引導我接近該地區的救助。

+0

我不知道我看到的問題。使用'{}'並在div內插入它有什麼問題? – Li357

+0

我試圖把所有的東西放在渲染方法裏面的花括號裏面都會引發錯誤。通常某種「那件事沒有定義」的錯誤。 – luskmonster

+0

'axios.get('https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=Stackoverflow') .then(function(response){ console.log(response.data) ; })。catch(function(e){console.log(e)})'這是什麼輸出? – kurumkan

回答

1
import React, { Component } from "react"; 
import axios from "axios"; 

const URL = "https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow"; 

export default class App extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     items: [] 
    } 
    } 

    componentDidMount() { 
    var _this = this; 
    axios.get(URL) 
    .then(function(res){ 
     _this.setState({ 
     items: res.data.items 
     }); 
    }) 
    .catch(function(e) { 
     console.log("ERROR ", e); 
    }) 
    } 

    render() { 
    const renderItems = this.state.items.map(function(item, i) { 
     return <li key={i}>{item.title}</li> 
    }); 

    return (
     <ul className="App"> 
     {renderItems} 
     </ul> 
    ); 
    } 
} 
+0

輝煌!今天晚上我學到了很多東西。 – luskmonster

+0

如果您的問題解決了 - 將標記可幫助您解決問題的答案 – kurumkan

+0

兩種解決方案都非常出色,這是我最終使用的解決方案。謝謝! – luskmonster

2

您可以通過React的組件狀態和生命週期完成此操作。

閱讀關於這個位置:React State/Lifecycle

您可以將獲取的組件的componentDidMount函數調用,並有回調的設置狀態進行查看。

如果你使用獲取,您的組件可能是這樣的:

class App extends Component { 
constructor(props) { 
    super(props); 
    this.state = { 
    data: false 
    }; 
    this.receiveData = this.receiveData.bind(this); 
} 
componentDidMount() { 
    var _self = this; 
    fetch('https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow') 
    .then(function(res) { 
    return res.json(); 
    }).then(function(json) { 
    console.log(json); 
    _self.receiveData(json); 
    }); 
} 
receiveData(data) { 
    this.setState({data}); 
} 
render() { 
    return <div>{this.state.data}</div> 
} 
} 
+1

這絕對看起來就像我試圖去的地方,當我嘗試運行這個tho時,在控制檯中出現下面的錯誤。 錯誤:對象作爲React子項無效(找到:包含鍵{items,has_more,quota_max,quota_remaining})的對象)。如果您打算渲染一組兒童,請改用數組,或者使用React附加組件中的createFragment(object)來包裝對象。檢查「App」的渲染方法。 – luskmonster

+0

哦,拍攝,是的抱歉對象無法直接呈現在反應元素中。如果您只想查看數據,您可以'''

JSON.stringify(this.state.data)
'''將其打印爲一個字符串輸出。 –

+0

我可以看到一些東西! – luskmonster