javascript - 如何在函数调用/手动中调用反应路由器 DOM 链接
问题描述
我想做的高级任务是在函数调用中从 react-router-dom 调用链接。
我的用例:我有一个提交按钮,一旦点击它就会调用一个 POST API。我想检查 POST API 的响应,如果调用成功,它将转到我的链接到我的成功提交页面。如果这是一个错误,我想调用我的错误页面的链接。要考虑的一个因素是 POST API 需要大约 2 秒的时间来响应。
理想情况下,我希望 Link 组件不要直接位于提交按钮组件上。在我的 API 响应后,单击包含 Link 组件的按钮后,我想调用一个函数。
这是我当前的代码:
function SubmitFcn(){
const submitPostAPI = (async () => {await axios({
//data passed
})
.then(function (response) {
//here is where I capture the API response
})
.catch(function (error) {});
});
return (
<div>
<Link to="/SubmitPage" style={{ textDecoration: "none" }}>
<Button
disabled={btnDisabled}
id="submitButtonFormPOST"
variant="contained"
color="primary"
onClick={submitPostAPI}
data-testid="submitButtonelement"
>
{" "}
Submit
</Button>
</Link>
</div>
);
}
}
这是我希望我的代码类似的内容,问题是它没有编译,因为我错误地调用了 Link 组件。
function SubmitFcn(){
const submitPostAPI = (async () => {await axios({
//data passed
})
.then(function (response) {
//here is where I capture the API response
LinkFunction(response.status)
})
.catch(function (error) {});
});
return (
<div>
<Link to="/SubmitPage" style={{ textDecoration: "none" }}>
<Button
disabled={btnDisabled}
id="submitButtonFormPOST"
variant="contained"
color="primary"
onClick={submitPostAPI}
data-testid="submitButtonelement"
>
{" "}
Submit
</Button>
</Link>
</div>
);
}
}
function LinkStatus(result){
if(result === 201){
<Link to="/Submitsucess"/>}
else{
<Link to="/ErrorPage:/>
}
}
解决方案
使用history.push("/submitSuccess")
或history.push("/errorPage")
通过route props或react hookhistory
提供的对象。useHistory
您可能还希望移除Link
包裹按钮,因为这可能会干扰该组件保持安装状态。
如果组件直接由Route
组件渲染,那么它会接收路由 props,因此只需history
从 props 对象访问
props.history
否则使用useHistory
反应钩子
const history = useHistory();
所以组件可能看起来类似于其中任何一个
const MyComponent = ({ history }) => {
const submitPostAPI = async () => {
await axios({
//data passed
})
.then(function (response) {
linkStatus(response.status);
})
.catch(function (error) {
history.push("/errorPage"); // <-- go to error page here as well
});
};
function linkStatus(result) {
if (result === 201) {
history.push("/submitSuccess");
} else {
history.push("/errorPage");
}
}
return (
<div>
<Button
disabled={btnDisabled}
id="submitButtonFormPOST"
variant="contained"
color="primary"
onClick={submitPostAPI}
data-testid="submitButtonelement"
type="button"
>
Submit
</Button>
</div>
);
};
或者
const MyComponent = ( ) => {
const history = useHistory();
const submitPostAPI = async () => {
await axios({
//data passed
})
.then(function (response) {
linkStatus(response.status);
})
.catch(function (error) {
history.push("/errorPage"); // <-- go to error page here as well
});
};
function linkStatus(result) {
if (result === 201) {
history.push("/submitSuccess");
} else {
history.push("/errorPage");
}
}
return (
<div>
<Button
disabled={btnDisabled}
id="submitButtonFormPOST"
variant="contained"
color="primary"
onClick={submitPostAPI}
data-testid="submitButtonelement"
type="button"
>
Submit
</Button>
</div>
);
};
推荐阅读
- c# - Quartz.Net 调度程序在 Azure 中不起作用
- coq - Coq 中 Reals 的第一个证明
- python - Python 给出: ConnectionResetError: [WinError 10054] 一个现有的连接被远程主机强行关闭
- python - 我的服务器上的 Python 请求有问题
- io - 文件未显示在通过 tcl 脚本生成的文件夹中
- javascript - 字段不应只接受空格
- c# - 在 HoloLens 1.MRTK 2.3 和 unity 2018 上将数据写入文本文件以进行构建的 Microsoft 标准方法是什么
- javascript - 如何根据打字稿中的键从对象中提取特定元素
- javascript - 我想在音频播放器中淡出我的 div
- javascript - cookie 未显示在开发人员工具上,但显示在挂锁图标的弹出窗口中