【问题标题】:Can't add tracing functionality in Redux with Next.js无法使用 Next.js 在 Redux 中添加跟踪功能
【发布时间】:2020-09-10 12:59:02
【问题描述】:

我想将tracing functionality 添加到我在 Redux 中的开发工具中:

但是我这样做的时候:

export default () => {
  let store;
  const isClient = typeof window !== 'undefined';
  if (isClient) {
    const { persistReducer } = require('redux-persist');
    const storage = require('redux-persist/lib/storage').default;
    const persistConfig = {
      key: 'root',
      storage,
      stateReconciler: autoMergeLevel2,

      whitelist: ['users', 'ui'] // place to select which state you want to persist
    };

   ******composeEnhancers******

    var composeEnhancers =
      typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
        ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
            // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
          })
        : compose;

    composeEnhancers = composeWithDevTools({
      actionCreators,
      trace: true,
      traceLimit: 25
    });

    store = createStore(
      persistReducer(persistConfig, rootReducer),
      composeEnhancers(
        applyMiddleware(thunkMiddleware, createLogger({ collapsed: false }))
      )
    );

  ******composeEnhancers******

    store.__PERSISTOR = persistStore(store);
  } else {
    store = createStore(
      rootReducer,
      composeEnhancers(
        applyMiddleware(thunkMiddleware, createLogger({ collapsed: false }))
      )
    );
  }
  return store;
};

我收到以下错误...

Unhandled Runtime Error
TypeError: composeEnhancers is not a function

Call Stack
module.exports../store/index.js.__webpack_exports__.default
file:///Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/hillfinder/.next/server/static/development/pages/_app.js (499:84)
createStore
file:///Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/hillfinder/node_modules/next-redux-wrapper/lib/index.js (95:20)
initStore
file:///Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/hillfinder/node_modules/next-redux-wrapper/lib/index.js (98:20)
Object.<anonymous>
file:///Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/hillfinder/node_modules/next-redux-wrapper/lib/index.js (137:33)
step
file:///Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/hillfinder/node_modules/next-redux-wrapper/lib/index.js (56:23)
Object.next
file:///Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/hillfinder/node_modules/next-redux-wrapper/lib/index.js (37:53)
<unknown>
file:///Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/hillfinder/node_modules/next-redux-wrapper/lib/index.js (31:71)
new Promise
<anonymous>

提前致谢!

更新

关注了redux-toolkit 的一个帖子,我想我已经完成了一半, 但现在我得到以下内容,考虑到我的设置,这似乎很奇怪:

Error: "reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers

这是我更新的商店:

import { combineReducers } from 'redux';
import { configureStore } from '@reduxjs/toolkit';
import { createLogger } from 'redux-logger';

/* imported reducers */
import ui from './reducers/ui/index';
import users from './reducers/users/index';

import thunkMiddleware from 'redux-thunk';
import { persistStore } from 'redux-persist';

import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import { persistReducer } from 'redux-persist';

var reducers = combineReducers({
  users: users,
  ui: ui
});

export default () => {
  let store;
  const isClient = typeof window !== 'undefined';
  if (isClient) {
    var storage = require('redux-persist/lib/storage').default;
    var persistConfig = {
      key: 'root',
      storage,
      stateReconciler: autoMergeLevel2,

      whitelist: ['users', 'ui'] // place to select which state you want to persist
    };

    var persistedReducer = persistReducer(persistConfig, reducers);

    store = configureStore({
      reducer: persistedReducer,
      middleware: [thunkMiddleware, createLogger()]
    });
    store.__PERSISTOR = persistStore(store);
  } else {
    store = configureStore({
      reducer: persistedReducer,
      middleware: [thunkMiddleware, createLogger()]
    });
  }
  return store;

【问题讨论】:

    标签: javascript redux redux-devtools redux-toolkit redux-devtools-extension


    【解决方案1】:

    我特别推荐改用the configureStore API from our official Redux Toolkit package。它已经默认打开了这个选项,它也会用良好的默认设置来处理你商店的其余部分。

    更新

    该错误听起来好像您实际上并未将reducer 字段传递给configureStore

    根据您更新的代码,我很确定这个 else 块是错误的:

        store = configureStore({
          reducer: persistedReducer,
          middleware: [thunkMiddleware, createLogger()]
        });
    

    根据您的逻辑,persistedReducer 仅存在于 if 块中,因此它在 else 块中是 undefined

    此外,您对 middleware 参数的使用目前忽略了开发检查中间件的所有自动使用。

    我建议将其重写为:

    export default () => {
      let rootReducer;
      const isClient = typeof window !== 'undefined';
      if (isClient) {
        // redux-persist setup logic here
        rootReducer = persistReducer(persistConfig, reducers);
      } else {
        rootReducer = reducers;
      }
    
      const store = configureStore({
        reducer: rootReducer,
         middleware: [...getDefaultMiddleware(), createLogger()]
      })
    
      return store
    }
    

    【讨论】:

    • 非常感谢!我深入研究了那里的问题,发现人们也在使用redux-persist,但是当我更新我的问题时,我得到了Error: "reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers...有什么想法吗?
    • 哇,谢谢我的朋友!这太棒了!现在只需安装工具包,我就可以在控制台中获得有关我的商店/状态的有用信息。谢谢!顺便说一句,您知道是什么原因造成的吗?stackoverflow.com/questions/61987537/…
    • 我并不是想把你拖进兔子洞,但我很好奇,因为你可能有一些见识...... :)
    猜你喜欢
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2018-10-22
    • 2013-07-21
    • 2021-02-09
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多