setState和提取后映射

时间:2018-11-22 08:43:07

标签: reactjs fetch setstate

请启发我的无知,我找不到正确呈现此Json响应的方法。在这里,我得到了一个我知道的对象的集合,但是我找不到解决我问题的正确答案。

import React, { Component } from 'react';

class App extends Component {
    constructor(props) {
        super(props);

        this.state = {
            data: []
        };
    }

    componentWillMount() {
        fetch('http://127.0.0.1:3000/')
        .then(response => response.json())
        .then(json => {
            console.log(json);
            /*/ setState in data /*/
        });
    }

    render() {
        return (
            <div>
                { /*/ Map here ? /*/ }
            </div>
        );
    }
}

export default App;

console.log(json); 给了我什么:

(3) [{…}, {…}, {…}]
0: {_id: "5bf6145ef4545c6ad22b48bd", id: 1, what: "Eat", when: "Tomorrow"}
1: {_id: "5bf61476f4545c6ad22b48be", id: 2, what: "Feel", when: "Always"}
2: {_id: "5bf62946f23cea02b52b821a", id: 3, what: "Die", when: "Never"}
length: 3
__proto__: Array(0)

0 个答案:

没有答案