2017-04-30 175 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