在 Redux 中,状态实际上存储在哪里? [英] In Redux, where does the state actually get stored?

查看:34
本文介绍了在 Redux 中,状态实际上存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题进行了一些搜索,但发现了非常模糊的答案.在 redux 中,我们知道状态是作为对象存储的.但是这个状态实际上存储在哪里呢?它是否以某种方式保存为可供我们稍后访问的文件?我所知道的是它不会将其存储在 cookie 格式或浏览器的本地存储中.

I searched a bit about this question but found very vague answers. In redux, we know that the state is stored as an object. But where is this state stored actually? Is it somehow saved as a file which can be accessed by us later on? What I know is that it does not store it in a cookie format or in the browser's local storage.

推荐答案

Redux 中的状态存储在内存中,在 Redux 商店中.

The state in Redux is stored in memory, in the Redux store.

这意味着,如果您刷新页面,该状态就会消失.

This means that, if you refresh the page, that state gets wiped out.

你可以想象这家商店看起来像这样:

You can imagine that store looking something like this:

function createStore(reducer, initialState) {
  let state = initialState // <-- state is just stored in a variable that lives in memory

  function getState() {
    return state
  }

  function dispatch(action) {

    state = reducer(state, action) // <-- state gets updated using the returned value from the reducer

    return action
  }

  return {
    getState,
    dispatch
  }
}

redux 中的状态只是一个在内存中持久化的变量,因为它被引用(通过 关闭) 由所有 redux 函数.

The state in redux is just a variable that persists in memory because it is referenced (via closure) by all redux functions.

这是正在发生的事情的简化示例:

Here's a simplified example of what is going on:

function example() {
  let variableAvailableViaClosure = 0
  
  function incrementTheClosureVariable() {
    variableAvailableViaClosure += 1
  }

  function getTheClosureVariable() {
    return variableAvailableViaClosure
  }

  return {
    incrementTheClosureVariable,
    getTheClosureVariable
  }
}

let data = example()

// at this point example is finished
// but the functions it returned
// still have access to the (internal) variable via closure

console.log(
  data.getTheClosureVariable() // 0
)

data.incrementTheClosureVariable()

console.log(
  data.getTheClosureVariable() // 1
)

此外,声明

在 redux 中,我们知道状态是作为对象存储的.

In redux, we know that the state is stored as an object.

不正确.redux 中的状态可以是任何有效的 javascript 值,而不仅仅是一个对象.通常将它作为一个对象(或像数组这样的特殊对象)是最有意义的,因为这允许更灵活的数据结构(但你可以让状态只是一个数字,例如,如果你想的话).

isn't correct. State in redux can be any valid javascript value, not just an object. It just usually makes the most sense for it to be an object (or a special object like an array) because that allows for a more flexible data structure (but you could make the state just be a number for example, if you wanted to).

查看实际的 Redux 实现了解更多详情.

Check out the actual Redux implementation for more details.

如果您希望状态保留在 cookie 或 localStorage 中,您可以 增强存储,这样,除了更新内存中的状态外,它还将保存到您想要的存储中(并在存储初始化时从该存储中加载)

If you want the state to persist in a cookie or localStorage, you would enhance the store such that, on top of updating the state in memory, it will save to your desired storage as well (and load from that storage when the store is initialized)

这篇关于在 Redux 中,状态实际上存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆