[英]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.