react实现跳转前记住页面状态的方法:1、监听path变化,当path变化时更新lastPath和currentPath到redux store中;2、在离开页面A时,将页面状态保存到redux store中;3、如果redux store中的lastPath等于页面B的path,则认为A是由B返回还原状态,否则不还原。
本教程操作环境:Windows10系统、react18.0.0版、Dell G3电脑。
react跳转前记住页面状态怎么实现?
React 页面返回保留上次状态
需求
页面A跳转到页面B然后再返回页面A,页面A要还原离开前的状态;
页面A和页面B有多个入口,从其它页面跳转到页面A,页面A不还原状态。
设计
监听path变化,当path变化时更新lastPath和currentPath到redux store中;
离开页面A时,将页面状态保存到redux store中;
进入页面A时,如果redux store中的lastPath等于页面B的path,则认为A是由B返回还原状态,否则不还原。
实现
项目采用react-router + dva库,实现部分会涉及相关技术。
监听path变化,通过history监听path变化,并记录lastPath和currentPath。这里采用dva的subscriptions,订阅history,当path变化同步path信息到state。
const model = { namespace: "global", state: { pathName: { last: "", current: "" }, }, reducers: { setPathName(state: any, { pathName }: any) { state.pathName.last = state.pathName.current; state.pathName.current = pathName; }, effects: { }, subscriptions: { setup({ history, dispatch }: any) { return history.listen(({ pathName }: any) => { dispatch({ type: "global/setPathName", pathName }); }); } } };
页面卸载时同步状态到redux store,比如:
componentWillUnmount() { const { dispatch } = this.props; const { activeKey } = this.state; dispatch({ type: "projectInfo/setProjectInfoPage", payload: { activeKey } }); }
页面重新加载时,比如:
state = { activeKey: pathToRegexp(PagePath.B).exec(pathName.last) ? activeKey : "" };
pathToRegexp来自path-to-regexp库,用于路由匹配,此处用来判断上个页面是否为页面B。
其它方案
A页面是否由B页面返回的判断:B页面返回时添加state,history.push({ pathname: path, state: {from} });,进入A页面根据state判断是否由B页面返回。但当B有多个入口,返回时需要知道页面来源,否则无法返回,逻辑稍显复杂且容易出错。
总结
本文提出了一种页面返回保留上次状态的解决方案,适用于页面有多个入口和出口的情况。该方案中采用了监听history变化的方式,记录上次页面地址,从而给是否还原状态提供了依据。
推荐学习:《react视频教程》
以上是react跳转前记住页面状态怎么实现的详细内容。更多信息请关注PHP中文网其他相关文章!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互动,可及可访问。1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒体综合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityAncccossibility.2)和TagsallowsemplowsemplowseamemelesseamlessallowsemlessemlessemelessmultimedimeDiaiiaemediaiaembedwitWithItWitTplulurugIns.3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrevulnerabilityLikeClickLickLickLickLickLickjAckJackJacking.2)

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

使用ID选择器在CSS中并非固有地不好,但应谨慎使用。1)ID选择器适用于唯一元素或JavaScript钩子。2)对于一般样式,应使用类选择器,因为它们更灵活和可维护。通过平衡ID和类的使用,可以实现更robust和efficient的CSS架构。

html5'sgoalsin2024focusonrefinement和optimization,notnewfeatures.1)增强performandemandeffifice throughOptimizedRendering.2)risteccessibilitywithrefinedibilitywithRefineDatientAttributesAndEllements.3)expliencernsandelements.3)explastsecurityConcerns,尤其是withercervion.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供应,2)语义结构,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,简化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3汉化版
中文版,非常好用

Dreamweaver CS6
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)