简体   繁体   English

如何使用 react-router、浏览器历史记录存储页面 session?

[英]How to store a page session with react-router, Browser History?

I am creating a simple toDo app where I have 3 links to navigate to.我正在创建一个简单的 toDo 应用程序,其中有 3 个链接可供导航。 I am trying to store the current page data regardless of whichever page I navigate to without losing the current page data if I end up going back.我试图存储当前页面数据,无论我导航到哪个页面,如果我最终返回,不会丢失当前页面数据。 For example, I have 3 checkbox items in the tasks tab.例如,我在任务选项卡中有 3 个复选框项目。 If I navigate to Reminder tab and go back to tasks tab later, I want the same 3 checkboxes and data to still be there.如果我稍后导航到提醒选项卡和 go 回到任务选项卡,我希望相同的 3 个复选框和数据仍然存在。 But the problem is the page data gets deleted when I navigate back to the current page.但问题是当我导航回当前页面时页面数据被删除。 I am using react-router to do this and others have mentioned to use Browser History API, but I am unsure how to approach this?我正在使用 react-router 来执行此操作,其他人提到使用浏览器历史记录 API,但我不确定如何处理这个? Do I have to track the changes in the current tab whenever data changes with Browser History and pass the location.state as props in react-router, Route?每当数据随浏览器历史记录发生变化时,我是否必须跟踪当前选项卡中的变化并将 location.state 作为 react-router、Route 中的道具传递?

在此处输入图像描述

You have multiple options to do this and they are:您有多种选择可以做到这一点,它们是:

  • You can save data to the local storage and then display it later您可以将数据保存到本地存储,然后再显示
  • You can use ContextAPI您可以使用 ContextAPI
  • You can use Redux to store data and re-use it later您可以使用 Redux 来存储数据并在以后重复使用

As you are developing to-do app and if you want to use React functionality instead of Local storage then go for ContextAPI because it's simple to-do app and redux is not needed for this.当您正在开发待办事项应用程序时,如果您想使用 React 功能而不是本地存储,那么 go 用于ContextAPI ,因为它是简单的待办事项应用程序,并且不需要 redux。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM