一句流行的句式:先问是不是,再问怎么做
electron怎么就不支持Prompt
了?
我猜可能是你用的BrowserRouter
而BrowserRouter
是需要web server
支持的(e.q, nginx的try_files, webpack-dev-server的historyApiFallback)
如果你的electron调用的本地html文件, 那么BrowserRouter
就无效了, 建议使用HashRouter
或者MemoryRouter
=============================================================
你的描述让我做了回阅读理解
window.prompt不等于react-router-dom的Prompt
实际上,如果只是要弹一个确认框的话你之前的代码完全够用了
<Prompt message="Are u sure you want to leave?" />
如果要像prompt这样拿一些用户输入的话加一点变通的方法好了
Prompt总是返回false 调用一个异步方法进行处理, 比如:
const history = useHistory();
const [retValue, setRetValue] = useState(false);
const confirm = async (pathname: string, action: string) => {
if (pathname === history.location.pathname) return;
const { response, checkboxChecked } = await remote.dialog.showMessageBox(
remote.getCurrentWindow(),
{
message: `Redirect to ${pathname}`,
checkboxLabel: "aaa",
buttons: ["OK", "Cancel"],
}
);
if (checkboxChecked && response === 0) {
setRetValue(true);
history.push(pathname);
}
};
return (
<div>
<Prompt
message={(location, action) => {
!retValue && confirm(location.pathname, action);
return retValue;
}}
/>
</div>
);
showMessageBox
客户用任何你想用的手段替换,比如一个React Dialog组件
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…