如何从对象访问数据并将其输入组件?

时间:2019-04-01 06:17:55

标签: reactjs

如何访问此数据

{"rahulkesarvani959":true,"jogendra":true,"sahebali":true,"trinav":true,"zeeshan":true,"athiselvam":true,"shantanu":true,"shashi88988":true,"govind":true,"abhilash":true,"akashsaroj7180":true,"khaled":true,"vaibhav":true,"ajay":true,"aashish":true,"sagar":true,"sushil":true,"vishalkmbl555":true,"amar":true,"brajesh07gautam08":true,"del3vnf":true,"del2vnf":true,"aalokkyadav9988":true}

我已经将boy变量设置为全局变量,但是我无法在render函数中获取值。

let boy = [];
 componentWillMount() {
    axios
      .get("https://freshvnf-api.firebaseio.com/tracked-locations.json?shallow=true")
      .then(resp => {
        if (resp.status === 200) {
          this.boy = _.keys(resp.data);
          console.log(this.boy);
        }
        else {
          Modal.error({
            title: 'Error!',
            content: resp.message,
          });
        }
      });
  }

 render() {
    console.log(this.boy);
    return (
      <div className="App">
        <Layout className="dashboardLayout">

1 个答案:

答案 0 :(得分:0)

您应该在组件中有一个状态,而不是全局变量。然后,当您从API获得响应时,可以执行setState()来获取该数据。

请参阅React的official docs中的示例代码。