3 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
要從 devtools 中隱藏 Redux,請(qǐng)注意以下代碼:
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調(diào)整后僅在__REDUX_DEVTOOLS_EXTENSION_COMPOSE__客戶端和開(kāi)發(fā)模式中使用,否則代碼僅使用compose,這意味著它將對(duì)瀏覽器 devtools 隱藏。

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
這些人并沒(méi)有真正給出所需的答案,但我在 vanilla redux 的 redux 文檔中發(fā)現(xiàn)了自己,如果你devTools: true在 store.js 中傳遞了它,那么它可以在生產(chǎn)和開(kāi)發(fā)中工作,但你可以在此禁用它方法 :
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
這對(duì)我有用,因?yàn)楫?dāng)您進(jìn)行開(kāi)發(fā)時(shí),我也在使用 vanilla redux 只需制作devTools: true并運(yùn)行您的應(yīng)用程序就可以了
注意:正如@JamesPlayer 在評(píng)論(評(píng)論鏈接)中所說(shuō),如果您正在使用此解決方案將有效@reduxjs/toolkit

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果您使用redux-devtools-extension,您可以通過(guò)以下方式輕松配置您的商店:
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
));
開(kāi)發(fā)工具中的記錄器將在生產(chǎn)中禁用。代替developmentOnly,您可以添加logOnlyInProduction以查看生產(chǎn)中的日志。
添加回答
舉報(bào)