从REST API传递变量中的GET答案-React.js

时间:2018-11-07 14:02:05

标签: javascript reactjs

我有点反应。 我已经制作了一个REST API,可以发送这样的JSON

[{Key1: value1, Key2: value2},{Key1: value3, Key2: value4},{Key1: value5, Key2: value6}]

调用API的react组件如下所示

import React from "react";
import axios from "axios";

class State extends React.Component {

constructor(props) {
    super(props);
    this.state={
        graph_data:[{
            Indication: '',
            IsOpen: '',
            Number: 0
        }],
    };
}

componentDidMount() {
    const res=[]
    axios.get('http://127.0.0.1:8080/watch/1.0.0/state')
        .then(response => {
            for(let x = 0; x < response.data.length; x++) {
                res[x] = response.data[x];
                console.log(res[x])
            }
        });
       console.log(res[2])
}

render() {
    const { graph_data } =this.state.graph_data;
    return (
        <div className="graph-wrapper">
            <div className="graph">
                <h3>{ graph_data }</h3>
            </div>
        </div>
    )
}
}

export default State;

<h3></h3>中的graph_data值为空,如何使其获取JSON数据? console.log(res [x])向我显示列表的x值,但之后的console.log(res [2])为空 谢谢

0 个答案:

没有答案