简体   繁体   English

如何在 React 中保留组件的 state?

[英]How to preserve state of a component in React?

In my React project, I have a homepage which displays several blogs based on a topic.在我的 React 项目中,我有一个主页,其中显示了基于某个主题的多个博客。 These blogs are stored in the state by making an API call on the basis of topic.这些博客通过基于主题的 API 调用存储在 state 中。 On clicking on a blog, it takes me to the individual page for it.单击博客时,它会将我带到它的单个页面。 Now what I want is that when I press back from the individual blog page, the earlier state is restored, ie I get the same list of blogs related to that topic which I had before and I do not have to make another API call.现在我想要的是,当我从单个博客页面返回时,之前的 state 会恢复,即我得到与该主题相关的博客列表,这与我以前拥有的相同,我不必再进行 API 调用。 How can I do this?我怎样才能做到这一点?

I suggest you use React.Context allowing you to store data in a global state.我建议您使用 React.Context 允许您将数据存储在全局 state 中。

The official documentation is pretty well written.官方文档写的很好。

There are multiple options to preserve/restore the state while navigating the application.在导航应用程序时,有多个选项可以保留/恢复 state。 Sorted from easiest to the more difficult solutions:从最简单到更困难的解决方案排序:

1. Cache your API responses 1.缓存您的 API 响应

This is probably the simplest change you can make and prevents your application from doing the same API request and re-uses the first response in each subsequent request.这可能是您可以进行的最简单的更改,并防止您的应用程序执行相同的 API 请求并在每个后续请求中重复使用第一个响应。

The downside is that when your data changes, the cache needs to be invalidated.缺点是当你的数据发生变化时,缓存需要失效。 This can either be done manually or you can use a cache lifetime mechanism.这可以手动完成,也可以使用缓存生命周期机制。

https://developer.mozilla.org/en-US/docs/Web/API/Request/cache https://developer.mozilla.org/en-US/docs/Web/API/Request/cache

2. Wrap your application with Context 2. 用 Context 包装你的应用程序

React's state is preserved as long the component is mounted.只要安装了组件,就会保留 React 的 state。 You can take advantage of this principal and store your state in a wrapping component.您可以利用此原则并将 state 存储在包装组件中。 With React's Context you can use this state.通过React 的 Context ,你可以使用这个 state。

This is a low-level entry to Global State Management without installing dependencies.这是全局 State 管理的低级条目,无需安装依赖项。

3. Global State Management with libraries 3.全局State管理带库

The most future-proof, but somewhat difficult solution, is to use a global state management library like;最具前瞻性但有些困难的解决方案是使用全局 state 管理库,例如; Redux or Mobx ReduxMobx

How do you handle the current topic?你如何处理当前的话题? I'm thinking that it would be a good idea to manage the current topic in the route, like this:我认为在路由中管理当前主题是个好主意,如下所示:

  • /blogs/sports: list blogs related to sports topic. /blogs/sports:列出与体育主题相关的博客。
  • /blogs/tech: list blogs related to technology topic. /blogs/tech:列出与技术主题相关的博客。
  • etc... ETC...

This way you don't have to worry about state preservation, when you go back from a specific blog you can take the topic from the route and display only the blogs related to that topic.这样您就不必担心 state 的保存,当您从特定博客返回 go 时,您可以从路由中获取主题并仅显示与该主题相关的博客。

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

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