【发布时间】:2025-11-30 16:45:02
【问题描述】:
我的目标是在单击按钮的事件期间启动模式弹出窗口。我在对包含模态的函数进行 Axios API 调用时遇到问题。 API调用的流程如下:
首先:这是发出 Axios 发布请求的按钮 (addToCart)(工作正常)
<button className="button is-primary" onClick={addToCart}>
ADD TO CART
</button>
第二个:这是调用模态弹窗(Example())的Axios post请求(工作正常)
const addToCart = () => {
Axios.post(
//standard backend API call is made here
)
.then((res) => {
console.log("post called")
Example(); //I want to launch my modal when this state is reached
})
.catch((err) => {
if (err.response && err.response.status === 401) {
setIsLoggedIn(false);
}
});
第三:这是我想弹出但在渲染函数时遇到问题的模态(问题)
https://react-bootstrap.github.io/components/modal/
function Example() {
const [show, setShow] = useState(false);
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
return (
<>
<Button variant="primary" onClick={handleShow}>
Launch demo modal
</Button>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Modal heading</Modal.Title>
</Modal.Header>
<Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
Close
</Button>
<Button variant="primary" onClick={handleClose}>
Save Changes
</Button>
</Modal.Footer>
</Modal>
</>
);
}
render(<Example />);
我收到以下错误:
Line 38:3: 'render' is not defined no-undef
如何正确呈现我的模态弹出窗口?
【问题讨论】:
标签: javascript reactjs axios