站長(zhǎng)資訊網(wǎng)
最全最豐富的資訊網(wǎng)站

react怎么實(shí)現(xiàn)路由跳轉(zhuǎn)前確認(rèn)

react實(shí)現(xiàn)路由跳轉(zhuǎn)前確認(rèn)功能的方法:1、通過(guò)“import { Modal } from 'antd';”方法引入“antd”;2、使用Antd的“Modal.confirm”實(shí)現(xiàn)彈框;3、設(shè)置Form表單內(nèi)容即可。

react怎么實(shí)現(xiàn)路由跳轉(zhuǎn)前確認(rèn)

本教程操作環(huán)境:Windows10系統(tǒng)、react18.0.0版、Dell G3電腦。

react怎么實(shí)現(xiàn)路由跳轉(zhuǎn)前確認(rèn)?

react-router 跳轉(zhuǎn)前確認(rèn)Prompt使用

需求

頁(yè)面切換的時(shí)候,會(huì)遇到這樣的需求:切換時(shí)需要判斷內(nèi)容區(qū)域編輯后是否保存了, 若沒(méi)保存,則彈出提示框,提示保存。

react怎么實(shí)現(xiàn)路由跳轉(zhuǎn)前確認(rèn)

官網(wǎng)示例

react router中的Prompt可以實(shí)現(xiàn)這樣的功能。

Prompt示例:https://reactrouter.com/web/example/preventing-transitions Prompt文檔:https://reactrouter.com/core/api/Prompt
登錄后復(fù)制

/** when:是否啟用 */ /** message:string | func */ // 示例1 <Prompt   when={formIsHalfFilledOut}   message="Are you sure you want to leave?" /> // 示例2 <Prompt   message={(location, action) => {     if (action === 'POP') {       console.log("Backing up...")     }     return location.pathname.startsWith("/app")       ? true       : `Are you sure you want to go to ${location.pathname}?`   }} />
登錄后復(fù)制

實(shí)現(xiàn)

我們項(xiàng)目的技術(shù)棧umi+antd+react

彈框用的Antd的 Modal.confirm

import React, { useEffect, useState } from 'react'; import { Modal } from 'antd'; import { useBoolean } from '@umijs/hooks'; // umi里封裝了該組件 // 或者 import { Prompt } from "react-router-dom"; import { useParams, history, Prompt } from 'umi'; import {   ExclamationCircleOutlined } from '@ant-design/icons'; import {  isEqual } from '@/utils/utils'; import { FormInstance } from 'antd/lib/form'; export default function BaseInfo() {   const { id } = useParams<{ id: string }>();    // 保留原始數(shù)據(jù)   const [orginData, setOrigin] = useState({});   // 修改后的數(shù)據(jù)   const [modifyData, setModify] = useState({});   // 是否啟用Prompt   const { state, setTrue, setFalse } = useBoolean(false);   // 還原信息 useLoading是自己封裝的hooks   const [isFetching, fetchInfo] = useLoading(getServiceGroupDetail);   useEffect(() => {     (async () => {       try {         if (id !== '0') {           const info = await fetchInfo(id);           setOrigin({             ...info            });           setModify({             ...info            });                   }       } catch (e) {         console.error(e);       }     })();   }, [id]);   useEffect(() => {     if (isEqual(orginData, modifyData)) {       setFalse();     } else {       setTrue();     }   }, [orginData, modifyData]);   const nextStep = (pathname?: string) => {     setFalse();     pathname &&       setTimeout(() => {         history.push(pathname);       });   };   return (       {/* 這里原來(lái)放的Form表單內(nèi)容 */}       {routerWillLeave(state, form, nextStep)}   ); } function routerWillLeave(   isPrompt: boolean | undefined,   formInstance: FormInstance, // 保存,我這個(gè)頁(yè)面是Form表單   nextStep: (pathname?: string) => void ) {   return (     <div>       <Prompt         when={isPrompt}         message={(location) => {           if (!isPrompt) {             return true;           }           Modal.confirm({             icon: <ExclamationCircleOutlined />,             content: '暫未保存您所做的更改,是否保存?',             okText: '保存',             cancelText: '不保存',             onOk() {               formInstance?.submit();               nextStep(location.pathname);             },             onCancel() {               nextStep(location.pathname);             }           });           return false;         }}       />     </div>   ); }
登錄后復(fù)制

推薦學(xué)習(xí):《react視頻教程》

贊(0)
分享到: 更多 (0)
網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
亚洲午夜成人精品无码色欲| 亚洲AV无码精品色午夜在线观看| 久久精品99无色码中文字幕| 日韩乱码人妻无码中文视频| 日产精品一线二线三线芒果| 免费精品久久天干天干| jiucao在线观看精品| 日韩a级片在线观看| 亚洲日韩在线视频| 日韩精品无码免费视频 | 久久精品国产99国产精品导航| 亚洲精品综合一二三区在线 | 久久香蕉国产线看观看精品yw| 青青精品视频国产| 中文字幕在线久热精品| 日韩在线视精品在亚洲| 国产精品美女在线观看| 国产精品亚洲自在线播放页码| 亚洲精品日韩专区silk| 2021午夜国产精品福利| 97精品久久天干天天蜜| 久久国产亚洲精品无码| 久久99精品国产自在现线小黄鸭| 999精品视频在线观看热6| 亚洲欧洲国产日韩精品| 少妇精品久久久一区二区三区| 亚洲精品私拍国产福利在线| 午夜精品美女写真福利| 久久综合国产乱子伦精品免费| 乱精品一区字幕二区| 99久久亚洲精品无码毛片| 97视频精品全国在线观看| 久久99精品久久久久久hb无码| 69久久夜色精品国产69小说| 亚洲精品国产第1页| 国产高清精品入口91| 无码专区人妻系列日韩精品少妇| 精品久久久久久无码不卡| 亚洲国产美女精品久久久| 国产精品露脸国语对白| 国产成人精品无码片区在线观看|