来自讨论here看来Redux reducer 的状态应该保存在数据库中。

在这种情况下,用户身份验证之类的功能如何工作?

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

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

编辑:我创建了一个 example Redux auth project这也恰好体现了通用 Redux 以及 Redux、Socket.io 和 RethinkDB 的实时更新。

请您参考如下方法:

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

是否持久化状态,这可能根本不是 Redux 关心的问题。这更多取决于应用程序逻辑。

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

由于网络调用是异步的,而 Redux 是同步的 - 你需要引入额外的中间件,如redux-thunkredux-promise .

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

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 并在下次应用程序运行时重新水合。


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!