亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在生產構建或斷開連接中排除/禁用 Redux devtools?

如何在生產構建或斷開連接中排除/禁用 Redux devtools?

HUWWW 2022-06-05 10:54:37
顧名思義,devtools 應該只在開發過程中可見或可訪問,而不是在生產過程中。我不希望我的最終用戶玩弄 state 和 dispatcher 或者看到幕后發生的事情。有沒有辦法在生產構建中隱藏 Redux Devtools 或斷開它?我正在尋找 Vanilla Redux 的答案。不是 Redux Saga、Redux Thunk 或 Rematch。
查看完整描述

3 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

要從 devtools 中隱藏 Redux,請注意以下代碼:


import { createStore, applyMiddleware, compose } from 'redux';

import createSagaMiddleware from 'redux-saga';

import reducer from '~/redux/reducers';

import mySaga from '~/redux/sagas';

import { nodeEnv } from '~/utils/config';


const composeEnhancers =

  (nodeEnv !== 'production' &&

    typeof window !== 'undefined' &&

    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||

  compose;


const sagaMiddleware = createSagaMiddleware();


export default createStore(

  reducer,

  composeEnhancers(applyMiddleware(sagaMiddleware))

);


sagaMiddleware.run(mySaga);

它是 Redux 和 Redux-Saga 之間的集成,并不重要的是:


const composeEnhancers =

  (nodeEnv !== 'production' &&

    typeof window !== 'undefined' &&

    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||

  compose;

composeEnhancers調整后僅在__REDUX_DEVTOOLS_EXTENSION_COMPOSE__客戶端和開發模式中使用,否則代碼僅使用compose,這意味著它將對瀏覽器 devtools 隱藏。


查看完整回答
反對 回復 2022-06-05
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

這些人并沒有真正給出所需的答案,但我在 vanilla redux 的 redux 文檔中發現了自己,如果你devTools: true在 store.js 中傳遞了它,那么它可以在生產和開發中工作,但你可以在此禁用它方法 :


import { configureStore } from '@reduxjs/toolkit';

import userReducer from '../features/userSlice';

import chatReducer from '../features/chatSlice';


export default configureStore({

  reducer: {

    user: userReducer,

    chat: chatReducer,

  },

  devTools: false,

});


上面的代碼是 store.js


這對我有用,因為當您進行開發時,我也在使用 vanilla redux 只需制作devTools: true并運行您的應用程序就可以了


注意:正如@JamesPlayer 在評論(評論鏈接)中所說,如果您正在使用此解決方案將有效@reduxjs/toolkit


查看完整回答
反對 回復 2022-06-05
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

如果您使用redux-devtools-extension,您可以通過以下方式輕松配置您的商店:


import { createStore, applyMiddleware } from 'redux';

import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';


const composeEnhancers = composeWithDevTools({

  // options like actionSanitizer, stateSanitizer

});

const store = createStore(reducer, /* preloadedState, */ composeEnhancers(

  applyMiddleware(...middleware),

  // other store enhancers if any

));

開發工具中的記錄器將在生產中禁用。代替developmentOnly,您可以添加logOnlyInProduction以查看生產中的日志。


查看完整回答
反對 回復 2022-06-05
  • 3 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號