简体   繁体   English

如何使用React和Axios将this.data传递给其他组件

[英]How do I pass this.data through to other components with React and Axios

I am running a client and server setup (react and axios api calls) And I would like to understand how to access the returned data from my child components within the React Framework. 我正在运行客户端和服务器设置(react和axios api调用),并且我想了解如何从React框架中的子组件访问返回的数据。 I have the connection working to the http server, however i lack the foundational knowledge of working with this.state or props. 我可以连接到http服务器,但是我缺乏使用this.state或props的基础知识。

here is effectively my app.js 这实际上是我的app.js

import React, { Component } from 'react';
import axios from 'axios';
import ChildComponent from "./components/childComponent"

class App extends Component {
 state = {
  data: [],
  intervalIsSet : false
};

  componentDidMount() {
    this.getDataFromDb();
    if (!this.state.intervalIsSet) {
      let interval = setInterval(this.getDataFromDb, 10000);
      this.setState({ intervalIsSet: interval });
    }
  }

  getDataFromDb = () => {
    fetch("http://localhost:3001/api/getData")
      .then(data => data.json())
      .then(res => this.setState({ data: res.data }));
  };

 render() {
    const { data } = this.state;
    return (
       <div>
         <childComponent />
       </div>

  );
};
}

export default App;

and here is the child component. 这是子组件。 --> my intention is to simply access (or print out) my returned data from the server from within the child component. ->我的目的是简单地从子组件中访问(或打印出)服务器返回的数据。

import React, { Component } from 'react'; 从'react'导入React,{组件};

class ChildComponent extends React.Component {
    render() {
      return (
            {this.props.data}
      );
    }
  }

  export default ChildComponent;

You can pass down the data array as the data prop to the child component. 您可以将data数组作为data支柱传递给子组件。 Make sure you also uppercase the first character in the component name, or it won't work properly. 确保您还将组件名称中的第一个字符也大写,否则它将无法正常工作。

A component needs to render a single top level element, so you could eg render a div with the JSON stringified data prop inside of it in the child component. 组件需要呈现一个顶级元素,因此您可以例如在子组件中呈现一个带有JSON字符串化data道具的div

class App extends React.Component {
  // ...

  render() {
    const { data } = this.state;
    return (
      <div>
        <ChildComponent data={data} />
      </div>
    );
  }
}

class ChildComponent extends React.Component {
  render() {
    return <div>{JSON.stringify(this.props.data)}</div>;
  }
}

First make sure you uppercase the first letter of ChildComponent . 首先确保您将ChildComponent的第一个字母uppercase If you want to pass data you should add that object as an attribute to the component , and then access it throught this.props . 如果要传递数据,则应将该对象作为属性添加到component ,然后通过this.props访问它。 Also you need to render a single top element, and if you don't need div or any other html element, you can wrap it with React.Fragment . 另外,您还需要呈现一个top元素,如果不需要div或任何其他html元素,则可以使用React.Fragment进行包装。

Regarding to data , if its an array you can simply map through it and return data you want to see, if you want the entire object to show as a string, you can use JSON.stringify() . 关于data ,如果它是一个数组,则可以简单地通过它map并返回要查看的数据,如果要使整个对象显示为字符串,则可以使用JSON.stringify() And if that's an object you can show only data you want. 如果这是一个object ,则只能显示所需的数据。

class App extends React.Component {
  //...
  render() {
    const { data } = this.state;
    return (
      <div>
        <ChildComponent data={data} />
      </div>
    );
  }
}

//for array, ex: data = ["first","name"];
class ChildComponent extends React.Component {
  render() {
    return (
      <React.Fragment>
        {this.props.data.map(item => 
          <p>{item}</p>
        )}
      </React.Fragment>
    );
  }
}

//for object, ex: data = {id: 1, name: 'John'};
class ChildComponent extends React.Component {
  render() {
    const {data} = this.props;

    return (
      <React.Fragment>
        <p>{data.id}</p>
        <p>{data.name}</p>
      </React.Fragment>
    );
  }
}

//for single value (string, int, etc), ex: data = "my name";
class ChildComponent extends React.Component {
  render() {
    return (
      <React.Fragment>
        <p>{this.props.data}</p>
      </React.Fragment>
    );
  }
}

//to show object as a string (could be any object mentioned before)
class ChildComponent extends React.Component {
  render() {
    return (
      <React.Fragment>
        {JSON.stringify(this.props.data)}
      </React.Fragment>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM