提问人:KaMZaTa 提问时间:4/9/2022 更新时间:4/10/2022 访问量:904
如何防止付款后直接访问成功页面?
How to prevent direct access to success page after the payment?
问:
我正在使用React 18.0,React Router Dom(v6),并且我正在尝试实现PayPal结账。它运行良好,付款后,我将用户重定向到我的成功页面,其中包含用户的下载链接。问题是这个成功页面总是可以访问的。因此,我尝试在结帐后立即允许访问。之后,应将用户重定向到家庭。
我尝试这样做,但没有成功:
import { useEffect } from "react"
import { useLocation, useNavigate } from "react-router-dom"
import { getInvoiceUrl } from "../services/invoiceService"
function Success() {
// Get the state from navigate on the Checkout page
let { state, pathname } = useLocation()
const navigate = useNavigate()
// Check the state in order to redirect the user to the homepage if the state is empty
useEffect(() => {
if (state === null || state === undefined) {
navigate("/")
return
}
// Clear the state after the user exits from the Success page
return navigate(pathname, { replace: true })
})
if (state === null || state === undefined) {
return
}
console.log("state:", state)
return (
<>
<div>
Thank you!
</div>
<div>
Abbiamo appena inviato un'email a
<span>
{state.email}
</span>
con in allegato la tua ricevuta (per favore, controlla la cartella SPAM se non dovessi vederla).
</div>
<div>
Puoi anche scaricare la ricevuta facendo click qui:
<a download href={getInvoiceUrl()}>
Download
</a>
</div>
</>
)
}
export default Success
答:
1赞
Abbas Hussain
4/10/2022
#1
哈哈,我只是创建了问题的整个场景,猜猜你没有返回任何你需要的东西return null
错误:Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
试试这个
let { state, pathname } = useLocation();
const navigate = useNavigate();
useEffect(() => {
if (!state) {
navigate("/");
}
return function cleanup() {
navigate(pathname, { replace: true });
navigate("/");
};
}, []);
if (!state) {
return null;
}
return (
<div>
Abbiamo appena inviato un'email a<span>{state.email}</span>
con in allegato la tua ricevuta (per favore, controlla la cartella SPAM
se non dovessi vederla).
</div>
);
评论
0赞
KaMZaTa
4/10/2022
谢谢,但它没有用。如果我将零件和声明留在外面,即使在成功结帐后,用户也总是会被重定向到家中。不幸的是,返回 null 并没有改变任何内容。useEffect
if
0赞
Abbas Hussain
4/10/2022
固定!问题检查。
0赞
Abbas Hussain
4/10/2022
谢谢伙计!因为你,我也学到了新东西。
1赞
Abbas Hussain
4/10/2022
github.com/facebook/react/issues/22839 这就是问题所在,请删除索引文件中的 StrictMode 标签,然后看到一切都会正常工作,我正在使用 react 17 版本,这就是它在我的机器中工作的原因
1赞
KaMZaTa
4/10/2022
相反,返回组件时,即使不删除 .Navigate
navigate()
StrictMode
2赞
Drew Reese
4/10/2022
#2
对于条件,您缺少返回有效 JSX。返回此处向 React 表明没有什么可渲染的。state === null || state === undefined
null
function Success() {
// Get the state from navigate on the Checkout page
const { state } = useLocation();
const navigate = useNavigate();
// Check the state in order to redirect the user to the homepage if the state is empty
useEffect(() => {
if (!state) {
navigate("/", { replace: true }); // <-- redirect to prevent access
return;
}
});
if (!state) {
return null; // <-- return valid JSX
}
return ( ... );
}
或者,为了消除对 的需要,返回组件。useEffect
Navigate
function Success() {
// Get the state from navigate on the Checkout page
const { state } = useLocation();
if (!state) {
return <Navigate to="/" replace />; // <-- return Redirect
}
return ( ... );
}
评论
1赞
KaMZaTa
4/10/2022
即使不删除 !此外,在我看来,它更干净。Navigate
StrictMode
评论
state
state
{email: '[email protected]'}
navigate("/success", state)
useEffect
if