数据库的Redux状态持久性 [英] Redux state persistence with a database

查看:108
本文介绍了数据库的Redux状态持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从讨论中此处似乎Redux reducer的状态应该保存在数据库中.

From the discussion here it seems that the state of Redux reducers should be persisted in a database.

在这种情况下,类似用户身份验证的工作原理如何?

How does something like user authentication works in this instance?

是否不会创建一个新的状态对象来替换数据库中先前创建和编辑的每个用户(及其应用程序状态)的状态?

Wouldn't a new state object be created to replace the previous state in the database for every user (and their application state) created and edited?

是否会在前端使用所有这些数据并不断更新数据库中的状态?

Would using all of this data on the front end and constantly updating the state in the database be performant?

编辑:我创建了一个示例Redux身份验证项目也恰好是通用Redux的例证,并使用Redux,Socket.io和RethinkDB进行实时更新.

I've created an example Redux auth project that also happens to exemplify universal Redux, and realtime updating with Redux, Socket.io and RethinkDB.

推荐答案

从这里的讨论看来,Redux减速器的状态应该保存在数据库中.

From the discussion here it seems that the state of Redux reducers should be persisted in a database.

要保持状态还是不保持状态,可能根本就不需要Redux.这完全取决于应用程序逻辑.

To persist the state or not, it's likely not a concern of Redux at all. It's more up to application logic.

如果应用程序中发生了某些事情,例如将数据上传到服务器,显然您需要保存状态(或将状态的一部分保存到服务器).

If something happens in an application, like data upload to server, obviously you need to save state (or a slice of the state to a server).

由于网络调用是异步的,但是Redux是同步的-您需要引入其他中间件,例如 redux-thunk redux-promise .

Since network calls are asynchronous, but Redux is synchronous - you need to introduce additional middleware, as redux-thunk or redux-promise.

作为注册示例,您可能需要执行这些操作,

As sign-up example, you likely need that actions,

export function creatingAccount() {
  return { type: 'CREATING_ACCOUNT' };
}

export function accountCreated(account) {
  return { type: 'ACCOUNT_CREATED', payload: account };
}

export function accountCreatingFailed(error) {
  return { type: 'ACCOUNT_CREATING_FAILED', payload: error };
}

export function createAccount(data, redirectParam) {
  return (dispatch) => {
    dispatch(creatingAccount());

    const url = config.apiUrl + '/auth/signup';

    fetch(url).post({ body: data })
      .then(account => {
        dispatch(accountCreated(account));
      })
      .catch(err => {
        dispatch(accountCreatingFailed(err));
      });
  };
}

状态的某些部分,例如授权后的用户对象,可能会存储到localStore中,并在下一次应用程序运行时重新补水.

Some portion of state, e.g. user object after authorization, might be stored to localStore and re-hydrated on next application run.

这篇关于数据库的Redux状态持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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