本文介绍了Redux 状态 - 最大推荐大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个项目管理应用程序,但我仍然在为一个问题而苦苦挣扎 - 在 Redux 状态下存储什么以及按需"加载什么.

是否有任何已知的 Redux 状态的最大推荐大小"?几十千字节,几百千字节,以兆字节为单位,......?

解决方案

请记住,归根结底,Redux 只是如何"将信息保存在内存中的范例.它不会增加或减少您要跟踪的数据量.

举个例子:假设您的应用正在跟踪待办事项.在 Redux 之前,你有一个这种伪结构的 React 类:

class TaskList {状态 = { 任务:[] }componentWillMount =>goGetTheTasks().then(tasks => this.setState({ tasks }))render = 

{this.state.tasks.map()}

}

使用 Redux,您的商店看起来像这样:

{任务: []}

在这两种情况下,您都在内存中保存相同的数据(相同的任务数组).

也许您可以进一步分享您对状态变得过大的某些担忧.另请记住,您可以放入商店的任何东西,都可以从商店中取出.而且——您也不会被迫将每个字节都放在一个存储中.仍然想为某些事情使用组件状态?这取决于您——但您将失去从中央来源访问数据的优势.

I'm building a project management app and I'm still struggling with a question - what to store in Redux state and what load "on demand".

Is there any known "maximum recommended size" of a Redux state? Dozens of kilobytes, hundreds of kilobytes, units of megabytes, ...?

解决方案

Keep in mind that, at the end of the day, Redux is just a paradigm for "how" to save your information in memory. It doesn't add or reduce the amount of data you are keeping track of.

As an example: let's say your app was keeping track of to-do tasks. Before Redux, you had a React class of this pseudo-structure:

class TaskList {
  state = { tasks: [] }
  componentWillMount => goGetTheTasks().then(tasks => this.setState({ tasks }))
  render = <div>{this.state.tasks.map()}</div>
}

With Redux, your store looks like this:

{
    tasks: []
}

In both cases, you are saving the same data (the same tasks array) in memory.

Perhaps you could further share certain concerns that you have about your state growing too large. Also keep in mind that anything you can put in your store, you can take out of your store. And—you're not forced to put every byte inside your one store either. Still want to use component state for certain things? That's up to you—but you'll lose out on the advantages of having that data accessible from a central source.

这篇关于Redux 状态 - 最大推荐大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:16
查看更多