简体   繁体   English

如何使用第一个 API 通话中的数据并在我的第二个 API 通话中使用该数据?

[英]How to use a data from the 1st API call and use that in my 2nd API call?

 class TableData extends Component { state = { loading: true, TableData: [], show: false, }; handleClose = () => { this.setState({ show: false }); }; handleShow = () => { this.setState({ show: true }); }; componentDidMount() { const url = 'https://dev.meets.openhouse.study/meets_teachers/'; axios.get(url).then((res) => { this.setState({ TableData: res.data }); this.setState({ loading: false }); }); } render() { return ( <div> <Table> <thead> <tr> <th>Room Name</th> <th>Teacher Name</th> <th>Subject</th> <th>Class</th> </tr> </thead> {this.state.TableData.map((data) => ( <tr key={data.created_at}> <td>{data.room_name}</td> <td>{data.teacher.user.full_name}</td> <td>{data.subjects_str}</td> <td>{data.classes_str}</td> <Button variant="secondary" onClick={this.handleShow}> Details </Button> <Modal size="lg" aria-labelledby="contained-modal-title-vcenter" centered show={this.state.show} onHide={this.handleClose} > <Modal.Header closeButton> <Modal.Title>Modal heading</Modal.Title> </Modal.Header> <Modal.Body>reading this text in a modal.</Modal.Body> <Modal.Footer> <Button variant="secondary" onClick={this.handleClose}> Close </Button> </Modal;Footer> </Modal> </tr> ))} </Table> </div> ); } } export default TableData;

I am making two API calls;我正在拨打两个 API 电话; 1st one is used for rendering a list of data in a table format.第一个用于以表格格式呈现数据列表。 In each list there is a button.在每个列表中都有一个按钮。 And while clicking the button I am trying to fire the 2nd Api(with a data property from 1st one).在单击按钮时,我试图触发第二个 Api(具有第一个的数据属性)。 How can I achieve this in Reactjs?我怎样才能在 Reactjs 中实现这一点?

In the given code snippet - I am trying to view the data which is coming from the 1st API call and added a button to it.在给定的代码片段中-我试图查看来自第一个 API 调用的数据并向其添加了一个按钮。 now I need to Fire a 2nd API call on Button Clik, in this URL - https://dev.meets.openhouse.study/room_participants/ ${room_name} (room_name will get from 1st API call).现在我需要在这个 URL - https://dev.meets.openhouse.study/room_participants/ ${room_name} 中触发 Button Clik 的第二次 API 调用(room_name 将从第一个 API 调用中获取)。

We can bind an anonymous function for button onclick. From that method, we could call our component method like below.我们可以为按钮 onclick 绑定一个匿名的 function。从那个方法,我们可以像下面这样调用我们的组件方法。

<Button variant="secondary" onClick={() => this.getRoomInfo(data.room_name) }>

Full code snippet:完整代码片段:

class TableData extends Component {
      state = {
        loading: true,
        TableData: [],
        show: false,
      };

      handleClose = () => {
        this.setState({ show: false });
      };

      handleShow = () => {
        this.setState({ show: true });
      };

      getRoomInfo = (roomName) => {
        this.handleShow();
        const url = `https://dev.meets.openhouse.study/room_participants/${roomName}`
        axios.get(url).then((res) => {
            console.log(res)
        });
      }

      componentDidMount() {
        const url = 'https://dev.meets.openhouse.study/meets_teachers/';
        axios.get(url).then((res) => {
          this.setState({ TableData: res.data });
          this.setState({ loading: false });
        });
      }

      render() {
        return (
          <div>
            <Table>
              <thead>
                <tr>
                  <th>Room Name</th>
                  <th>Teacher Name</th>
                  <th>Subject</th>
                  <th>Class</th>
                </tr>
              </thead>

              {this.state.TableData.map((data) => (
                <tr key={data.created_at}>
                  <td>{data.room_name}</td>
                  <td>{data.teacher.user.full_name}</td>
                  <td>{data.subjects_str}</td>
                  <td>{data.classes_str}</td>
                  <Button variant="secondary" onClick={() => this.getRoomInfo(data.room_name) }>
                    Details
                  </Button>
                  <Modal
                    size="lg"
                    aria-labelledby="contained-modal-title-vcenter"
                    centered
                    show={this.state.show}
                    onHide={this.handleClose}
                  >
                    <Modal.Header closeButton>
                      <Modal.Title>Modal heading</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>reading this text in a modal!</Modal.Body>
                    <Modal.Footer>
                      <Button variant="secondary" onClick={this.handleClose}>
                        Close
                      </Button>
                    </Modal.Footer>
                  </Modal>
                </tr>
              ))}
            </Table>
          </div>
        );
      }
    }

    export default TableData;

暂无
暂无

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

相关问题 [javascript]使用来自第一个json的数据并将其转换为变量,然后使用该变量调用第二个json - [javascript]Use the data from 1st json and convert it to a variable and use that variable to call a 2nd json 如何将数据从第二数组添加到第一数组以在Google Visualization API中显示图表 - how to add data from 2nd array to 1st array for showing chart in google Visualization api 在第一个结束后在后台进行第二个 API 调用 - Make 2nd API call in the background after 1st one ends in an observable 为什么我的第二个javascript承诺调用返回与第一个相同的值? - Why does my 2nd javascript promise call return the same value as the 1st? 如何在第二个 API 函数中访问第一个 API 函数中的变量 - how to access the variables in 1st API function in the 2nd API function 从第一个填充第二个列表框 - populate 2nd listbox from 1st Function 内的第二个 AJAX 调用发生在第一个 AJAX 调用之前 - Function inside 2nd AJAX call is happening before the 1st AJAX call 在第一个json调用正常工作但第二个json调用不工作直到页面刷新 - After 1st json call working but 2nd json call not working till page get refresh 角异步诺言未正确链接以回馈第一个调用以进行第二个调用 - Angular async promise is not chaining correctly to give back 1st call in order to make 2nd call 如何根据从第一个下拉菜单自动更改的第二个下拉菜单更改第二个文本框的值? - How to change the 2nd textbox value based on the 2nd dropdown that is automatically changed from 1st dropdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM