首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Redux-持久化而不持久化

Redux-持久化而不持久化
EN

Stack Overflow用户
提问于 2019-01-01 16:25:26
回答 1查看 1.1K关注 0票数 0

我有一个反应/类型记录项目是从微软的类型记录-反应-启动项目开始的:

  • “反应”:"^16.7.0“
  • “反应”:"^16.7.0“
  • “redux-持久化”:"^5.10.0“
  • “打字本”:"^3.2.2“
  • “react redux”:"^6.0.0“

其目的是存储和持久化安全配置。

我已经跟踪了redux-persit文档,但是由于某种原因,这些信息并没有被持久保存。

我遗漏了什么?

redux/Security/Actions/ActionTypes.tsx:

代码语言:javascript
复制
export enum SecurityActionTypes {
  SAVE = 'SAVE'
}
export interface ISecurityAction {
  type: SecurityActionTypes.SAVE;
  payload: ISecurityPayload;
}

export interface ISecurityPayload {
  username: string;
  password: string;
  token: string;
}

redux/Security/Actions/ActionCreators.tsx:

代码语言:javascript
复制
import { ISecurityAction, ISecurityPayload, SecurityActionTypes } from './ActionTypes';

export function saveToken(param: ISecurityPayload): ISecurityAction {
  return {
    type: SecurityActionTypes.SAVE,
    payload: param
  };
}

redux/Security/Reducer/Reducer.tsx

代码语言:javascript
复制
import { ISecurityAction, ISecurityPayload, SecurityActionTypes } from '../../Actions/Security/ActionTypes';

const emptySecurity: ISecurityPayload = { username: '', password: '', token: '' };

export const initialState: ISecurityState = {
  security: emptySecurity
};

export interface ISecurityState {
  security: ISecurityPayload;
}

export function reducer(state = initialState, action: ISecurityAction) {
  switch (action.type) {
    case SecurityActionTypes.SAVE:
      return Object.assign({}, state, action.payload);
    default:
      return state;
  }
}

redux/Reducer/RootReducer.tsx

代码语言:javascript
复制
import { combineReducers } from 'redux';
import * as fromBreadCrumbs from './BreadCrumbs/Reducer';
import * as fromSecurity from './Security/Reducer';

export interface IRootState {
  breadCrumbsState: fromBreadCrumbs.IBreadCrumbsState;
  securityState: fromSecurity.ISecurityState;
}

export const rootInitialState: IRootState = {
  breadCrumbsState: fromBreadCrumbs.initialState,
  securityState: fromSecurity.initialState
};

export const rootReducer = combineReducers<IRootState>({
  breadCrumbsState: fromBreadCrumbs.reducer,
  securityState: fromSecurity.reducer
});

redux/Store/Store.tsx

代码语言:javascript
复制
import { createStore } from 'redux';
import { persistReducer, persistStore } from 'redux-persist';
import { IRootState, rootReducer } from '../Reducers/RootReducer';

import storage from 'redux-persist/lib/storage';

const persistConfig = {
  key: 'root',
  debug: true,
  storage
};

const persistedReducer = persistReducer<IRootState, any>(persistConfig, rootReducer);

export default () => {
  const store = createStore<IRootState, any, any, any>(persistedReducer);
  const persistor = persistStore(store);
  return { store, persistor };
};

src/index.tsx

代码语言:javascript
复制
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { PersistGate } from 'redux-persist/integration/react';
import App from './App';
import './index.css';
import registerServiceWorker from './registerServiceWorker';

import { BrowserRouter } from 'react-router-dom';

import { Provider } from 'react-redux';

import SecuredRoute from './components/SecuredRoute/SecuredRoute';

import CreateStore from './redux/Store/Store';

const { persistor, store } = CreateStore();

ReactDOM.render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <BrowserRouter>
        <SecuredRoute path="/" component={App} />
      </BrowserRouter>
    </PersistGate>
  </Provider>,
  document.getElementById('root') as HTMLElement
);
registerServiceWorker();
EN

回答 1

Stack Overflow用户

发布于 2019-01-01 17:40:29

我安装了redux-记录器,这有助于理解问题:-我在有效负载上出错了,因为我忘了添加“安全性”参数。

所作的修改如下:

redux/Security/Actions/ActionTypes.tsx:

代码语言:javascript
复制
export enum SecurityActionTypes {
  SAVE = 'SAVE'
}
export interface ISecurityAction {
  type: SecurityActionTypes.SAVE;
  payload: { security: ISecurityPayload };
}

export interface ISecurityPayload {
  username: string;
  password: string;
  token: string;
}

redux/Security/Actions/ActionCreators.tsx:

代码语言:javascript
复制
import { ISecurityAction, ISecurityPayload, SecurityActionTypes } from './ActionTypes';

export function saveToken(param: ISecurityPayload): ISecurityAction {
  return {
    type: SecurityActionTypes.SAVE,
    payload: {
      security: param
    }
  };
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53997048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档