簡體   English   中英

如何在頁面加載時保留我的 redux (redux-saga) 數據而不顯示/存儲在本地或 session 存儲中?

[英]How can I preserve my redux (redux-saga) data without display/store in local or session storage on page load?

請查看以下文件,其中設置了我的 redux 商店。
configureStore.js 文件

export default function configureStore(initialState) {
  const sagaMiddleware = createSagaMiddleware()
  const composeEnhancers =
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose

  const logger = store => next => action => {
    let result = next(action)
    try {
      const fullState = store.getState()
      localStorage.setItem("state", JSON.stringify(fullState));
    } catch (e) {
      console.error("localStorage set error:", e)
    }
    return result
  }

  const store = createStore(
    rootReducer,
    initialState,
    composeEnhancers(applyMiddleware(sagaMiddleware, logger))
  )
  sagaMiddleware.run(rootSaga)

  return store
}

請檢查我要在其中恢復我的 redux 應用程序加載信息的根index.js文件

let initialState = {}
try {
// set redux data in initial state
  initialState = JSON.parse(localStorage.getItem("state")) || {};
} catch (e) {
  console.error("localStorage get error:", e)
}
const store = configureStore(initialState)

const app = (
  <Provider store={store}>
    <BrowserRouter>
      <App />
      <Loader />
    </BrowserRouter>
  </Provider>
)

請幫我解決這個問題。 提前致謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM