简体   繁体   English

Redux TypeError:无法读取未定义的属性“应用”

[英]Redux TypeError: Cannot read property 'apply' of undefined

I have disabled react devtools and redux devtools.我禁用了 react devtools 和 redux devtools。

I've been searching for ways to deal with this problem for hours, and most of the problems are in compose while I don't change code at all.几个小时以来,我一直在寻找解决这个问题的方法,大多数问题都在撰写中,而我根本不更改代码。

import { createStore, applyMiddleware,compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const initialState={};

const middleware = [thunk];

const store = createStore(rootReducer,initialState,
    compose(
        applyMiddleware(...middleware),
        window.__REDUX_DEVTOOLS_EXTENSION__&& window.__REDUX_DEVTOOLS_EXTENSION__()
        )   
    );


export default store;

I really don't understand why this happened.我真的不明白为什么会这样。 I don't change anything and the last thing I do is just do git push origin master to my repository and suddenly when I compiled I got this error:我什么都没做,我做的最后一件事就是做 git 将原始主机推送到我的存储库,突然当我编译时我得到了这个错误:

Iam Using this in my front end:我在我的前端使用这个:

  "@material-ui/core": "^3.3.1",
    "@material-ui/icons": "^3.0.1",
    "axios": "^0.18.0",
    "jwt-decode": "^2.2.0",
    "prop-types": "^15.6.2",
    "react": "^16.6.0",
    "react-dom": "^16.6.0",
    "react-redux": "^5.1.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.0.5",
    "react-select": "^2.1.1",
    "recharts": "^1.3.5",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0",
    "typeface-roboto": "0.0.54"

Back-end:后端:

 "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "express": "^4.16.4",
    "mongoose": "^5.3.11",
    "multer": "^1.4.1",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "path": "^0.12.7",
    "validator": "^10.9.0",
    "xlsx": "^0.14.1"
  },
  "devDependencies": {
    "concurrently": "^4.0.1",
    "nodemon": "^1.18.6",
  },

Result Error:结果错误:

图像错误 1

Redux: Redux:

图像错误 2

Update your redux dev tools from 2.16.0 to 2.16.1将您的 redux 开发工具从 2.16.0 更新到 2.16.1

OR或者

Remove this line from your code从您的代码中删除此行

window.__REDUX_DEVTOOLS_EXTENSION__&& window.__REDUX_DEVTOOLS_EXTENSION__()

I had this same issue when I wanted to test my web app on an incognito window (extensions don't show up on incognito windows).当我想在隐身窗口上测试我的网络应用程序时,我遇到了同样的问题(扩展程序不显示在隐身窗口上)。

The issue is that compose from redux expects all its arguments to be functions.问题是compose from redux希望它的所有参数都是函数。 So when所以当

window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() 

is evaluated in that environment it returns a boolean.在该环境中评估它返回一个布尔值。

As @knutwalker mentioned.正如@knutwalker 提到的。 You'd need to return a function that returns nothing.您需要返回一个不返回任何内容的函数。 This fixed it for me,这为我修好了,

      window.__REDUX_DEVTOOLS_EXTENSION__
        ? window.__REDUX_DEVTOOLS_EXTENSION__()
        : f => f

The last stack shows a call to compose in client/src/store.js:9 where the second argument is window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() .最后一个堆栈显示了在client/src/store.js:9中对 compose 的调用,其中第二个参数是window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

If you have the devtools disabled however, __REDUX_DEVTOOLS_EXTENSION__ is undefined and becomes the second argument to compose function.但是,如果您禁用了 devtools,则__REDUX_DEVTOOLS_EXTENSION__未定义并成为 compose 函数的第二个参数。 It is still explicitly provided, which is different from it being implicitly undefined by omission, so the compose implementation thinks that there are two valid arguments and expects them to be functions, not undefined.它仍然是显式提供的,这与它通过省略隐式未定义不同,因此 compose 实现认为有两个有效参数并期望它们是函数,而不是未定义的。

You should return a dummy function in case there are no devtools available, something like the maybe, though I'm not quite sure what the exact signature must be to satisfy the createStore function.如果没有可用的开发工具,您应该返回一个虚拟函数,例如可能,尽管我不太确定确切的签名必须是什么才能满足 createStore 函数。

window.__REDUX_DEVTOOLS_EXTENSION__ ? window.__REDUX_DEVTOOLS_EXTENSION__() : (a) -> a

For me, I added the redux-devtools extension to Chrome and The error went away as soon as I added redux-devtools to the browser and you don't need to use ternary operator as well.对我来说,我向 Chrome 添加了 redux-devtools 扩展,一旦我将 redux-devtools 添加到浏览器,错误就消失了,您也不需要使用三元运算符。

https://github.com/reduxjs/redux/issues/2359#issuecomment-362340634 https://github.com/reduxjs/redux/issues/2359#issuecomment-362340634

The reason for this error is in the question itself.此错误的原因在于问题本身。 Just enable React and Redux DevTools extension .只需启用 React 和 Redux DevTools 扩展 It worked for me for the same error.对于同样的错误,它对我有用。

The best way I solved this issue is simple in my case.在我的情况下,我解决这个问题的最好方法很简单。

You know cypress opens chrome in developer mode, which does not have your extensions including redux devtools.您知道 cypress 在开发人员模式下打开 chrome,该模式没有您的扩展,包括 redux devtools。

So, when the window on chrome opens with the error click the menu > more tools > Extensions and go to chrome webstore from to install redux devtools .因此,当 chrome 上的窗口打开并出现错误时,单击菜单 > 更多工具 > 扩展程序,然后转到 chrome webstore 以安装redux devtools

i changed我变了

window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

to this:对此:

typeof window.__REDUX_DEVTOOLS_EXTENSION__ === "undefined"
? a => a
: window.__REDUX_DEVTOOLS_EXTENSION__ &&
    window.__REDUX_DEVTOOLS_EXTENSION__()

and the problem was solved问题解决了

This error is because of:这个错误是因为:

const store=createStore(rootreducer,
    initialstate,
    compose(applyMiddleware(...middleware),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())
)

While deploying your project you not did this:在部署您的项目时,您没有这样做:

window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() 

While running the command npm run build make sure you remove that.在运行命令npm run build确保将其删除。
The correct way is:正确的方法是:

const store=createStore(rootreducer,
    initialstate,
    compose(applyMiddleware(...middleware))
)

I faced a similar issue ,And removing these lines did the trick,我遇到了类似的问题,删除这些行就成功了,

 window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

but it is not a permanent solution as to use redux dev tools again you need these lines.但这不是一个永久的解决方案,因为再次使用 redux 开发工具你需要这些行。 Instead use而是使用

process.env.NODE_ENV==="development" ?
       window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() : compose,

it uses env variables to check weather to use Reduxdev tools middleware or not.它使用环境变量来检查天气以使用 Reduxdev 工具中间件与否。 as i didn't saw this solution in this thread so i posted it hope it'll be useful to somebody..因为我没有在这个线程中看到这个解决方案所以我发布了它希望它对某人有用..

In my case this error was causing blank/white screen in production build on mobile browser but not in desktop browser.在我的情况下,此错误导致在移动浏览器上的生产版本中出现空白/白屏,但在桌面浏览器中不会。 The accepted answer did not work for me, I had to remove the line, add the devtools extension, and refactor to use the composeWithDevTools import.接受的答案对我不起作用,我必须删除该行,添加 devtools 扩展,并重构以使用 composeWithDevTools 导入。 Which is to say, the answer from @Roman was a full working solution for me (thank you sir).也就是说,@Roman 的回答对我来说是一个完整的工作解决方案(谢谢先生)。 But it may help others to clarify that the problem can occur independent of redux-saga.但它可能有助于其他人澄清问题可以独立于 redux-saga 发生。 My app was just using basic redux-thunk.我的应用程序只是使用基本的 redux-thunk。 I installed redux-devtools-extension as a dev dependency, then replaced this:我安装了 redux-devtools-extension 作为开发依赖项,然后替换了这个:

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const initialState = {};

const middleware = [thunk];

if (nodeEnv !== 'production') {
  const store = createStore(rootReducer, initialState, compose(applyMiddleware(...middleware)));
} else {
  const store = createStore(
    rootReducer,
    initialState,
    compose(
      applyMiddleware(...middleware),
      window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
    ),
  );
}

export default store;

with this:有了这个:

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const initialState = {};

const middleware = [thunk];

const store = createStore(
  rootReducer,
  initialState,
  composeWithDevTools(applyMiddleware(...middleware))
);

export default store;

And that fixed it.这修复了它。

确保您已下载适用于 chrome 的redux-devtools扩展,并将站点访问权限更改为在所有站点特定站点上,方法是转到管理扩展 > Redux DevTools(详细信息)> 站点访问

This happened to me with version 2.17.0 of Redux DevTools.这发生在我的 Redux DevTools 2.17.0 版中。

Something got corrupted in the extension.扩展中的某些内容已损坏。

I just removed the extension from Chome, reinstalled it, and it worked like a charm.我刚刚从 Chome 中删除了扩展程序,重新安装了它,它就像一个魅力。

This worked for me:这对我有用:

This error is because of:这个错误是因为:

const store=createStore(rootreducer,
    initialstate,
    compose(applyMiddleware(...middleware),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())
)

While deploying your project remove the last line as follows:部署项目时,删除最后一行,如下所示:

window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

While running the command npm run build make sure you remove that.在运行命令 npm run build 时,请确保将其删除。 The correct way is:正确的方法是:

const store=createStore(rootreducer,
    initialstate,
    compose(applyMiddleware(...middleware))
)

Had the same error in Chrome incognito window.在 Chrome 隐身窗口中出现同样的错误。 Changed to normal Chrome and all worked fine.更改为普通 Chrome,一切正常。

This is a complete answer of @Koop that works for me:这是@Koop 对我有用的完整答案

For everyone experiencing the “TypeError: Cannot read property 'apply' of undefined” in node_modules/redux/es/redux.js: error: Immediate fix: Install Chrome extension Redux Devtools Permanent fix for all browsers:对于在 node_modules/redux/es/redux.js 中遇到“TypeError: Cannot read property 'apply' of undefined”的每个人:错误:立即修复:安装 Chrome 扩展 Redux Devtools 所有浏览器的永久修复:

  1. In the client directory: npm install --save-dev redux-devtools-extension在客户端目录中: npm install --save-dev redux-devtools-extension
  2. Change client/src/store/index.js to :将 client/src/store/index.js 更改为:

import { applyMiddleware, createStore, } from ‘redux’;
import createSagaMiddleware from ‘redux-saga’;

import rootReducer from ‘./reducers/index’;
import rootSaga from ‘./sagas/index’;
import { composeWithDevTools } from ‘redux-devtools-extension’;

const sagaMiddleware = createSagaMiddleware();
const store = createStore(
  rootReducer,
  composeWithDevTools(applyMiddleware(sagaMiddleware))
);
sagaMiddleware.run(rootSaga);
export default store;

changing this line改变这条线

window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

to

window.__REDUX_DEVTOOLS_EXTENSION__ ? window.__REDUX_DEVTOOLS_EXTENSION__() : f => f

will solve the issue and it worked for me to.将解决这个问题,它对我有用。

Before Error:出错前:

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const initialState = {};

const middleWare = [thunk];

const store = createStore(rootReducer, initialState, compose(
    applyMiddleware(...middleWare),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
));

export default store;

After remove error:删除错误后:

In Store.js we have to remove在 Store.js 中,我们必须删除

window.窗户。 REDUX_DEVTOOLS_EXTENSION && window. REDUX_DEVTOOLS_EXTENSION && 窗口。 REDUX_DEVTOOLS_EXTENSION () REDUX_DEVTOOLS_EXTENSION ()

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const initialState = {};

const middleWare = [thunk];

const store = createStore(rootReducer, initialState, compose(
    applyMiddleware(...middleWare)
));

export default store;

For answers saying to remove the following line of code对于说删除以下代码行的答案

window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()

Let's first know what does it do: It tries to check whether Chrome's Redux Devtools extension is available for the Redux store to connect to or not.首先让我们知道它的作用:它会尝试检查 Chrome 的 Redux Devtools 扩展是否可用于 Redux 商店连接。

Disable extension禁用扩展

  • Go to chrome://extensions . Go 到chrome://extensions
  • Locate and disable this extension.找到并禁用此扩展程序。
  • Refresh your app with Chrome Developer tools opened.使用打开的 Chrome 开发者工具刷新您的应用。
  • Type window.__REDUX_DEVTOOLS_EXTENSION__ in the console, it should return undefined .在控制台中window.__REDUX_DEVTOOLS_EXTENSION__ ,它应该返回undefined Note: This is also the case of Incognito window注意:这也是隐身 window 的情况

Enable the extension启用扩展

  • Go to chrome://extensions . Go 到chrome://extensions
  • Locate and enable this extension.找到并启用此扩展程序。
  • Refresh your app with Chrome Developer tools opened.使用打开的 Chrome 开发者工具刷新您的应用。
  • Type window.__REDUX_DEVTOOLS_EXTENSION__ in the console, it should return a function definition.在控制台中window.__REDUX_DEVTOOLS_EXTENSION__ ,它应该返回一个 function 定义。

IMO, it should be removed only if you are no more interested to use this extension for debugging purpose for your app. IMO,只有当您不再有兴趣使用此扩展程序来调试您的应用程序时,才应将其删除。 Disabling it does not/should not mean that we completely remove the ability in our code to connect for debugging purpose.禁用它并不/不应该意味着我们完全删除了代码中连接以进行调试的能力。


Next, once we know what does this line do, and we are interested to use this extension whenever we want for any future debugging, we should handle both the cases, when extension is available and when it is not available instead of removing the code itself to connect to it.接下来,一旦我们知道了这行代码的作用,并且我们有兴趣在以后的任何调试中使用这个扩展,我们应该处理这两种情况,当扩展可用和不可用时,而不是删除代码本身连接到它。

Basic Store setup基本商店设置

If you have a Basic Store setup as shown below ie it does not use middlewares or enhancers如果您有如下所示的基本商店设置,即它不使用中间件或增强器

const store = createStore(
   reducer, /* preloadedState, */
   window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  );

It checks if extension is available, if available call it and if not do nothing which means undefined .它检查扩展是否可用,如果可用则调用它,如果没有则什么都不做,这意味着undefined createStore method seems to properly handle undefined case and code works fine. createStore方法似乎可以正确处理undefined的情况并且代码工作正常。

Advanced Store setup高级商店设置

If you have a Advanced Store setup as shown below ie it uses middlewares or enhancers using applyMiddleware or you can say we use compose如果您有如下所示的Advanced Store 设置,即它使用中间件或使用applyMiddleware增强器,或者您可以说我们使用compose

const store = createStore(reducer, /* preloadedState, */ compose(
  applyMiddleware(...middleware)
));

Code to connect to the extension using compose is (This is what is the situation in the question)使用compose连接到扩展的代码是(这是问题中的情况)

const store = createStore(reducer, /* preloadedState, */ compose(
  applyMiddleware(...middleware),
  window.__REDUX_DEVTOOLS_EXTENSION__&& window.__REDUX_DEVTOOLS_EXTENSION__()
));

compose wants its arguments to be of function type and finding the devtools extension can return undefined . compose希望其 arguments 为function类型,并且发现 devtools 扩展可以返回undefined And for undefined cases, it will try to use undefined.apply method and throws an error.对于undefined的情况,它会尝试使用undefined.apply方法并抛出错误。 And this is the root cause of the issue.这是问题的根本原因。

Solution - Not so complete解决方案 - 不那么完整

As many answered, use window.__REDUX_DEVTOOLS_EXTENSION__? window.__REDUX_DEVTOOLS_EXTENSION__(): f => f正如许多人回答的那样,使用window.__REDUX_DEVTOOLS_EXTENSION__? window.__REDUX_DEVTOOLS_EXTENSION__(): f => f window.__REDUX_DEVTOOLS_EXTENSION__? window.__REDUX_DEVTOOLS_EXTENSION__(): f => f

For the undefined cases, it will run (f => f).apply which executes successfully.对于未定义的情况,它将运行(f => f).apply成功执行。 But this is more of fooling the compose method and looks hacky to me.但这更像是在欺骗compose方法,对我来说看起来很hacky。

Solution - Complete解决方案 - 完成

If there a way where I can decide to use compose or not, or a way to enhance the compose method.如果有一种方法可以让我决定是否使用 compose,或者有一种方法可以增强 compose 方法。

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(reducer, /* preloadedState, */ composeEnhancers(
  applyMiddleware(...middleware)
));

Please note that you need not now check window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() because it has been taken care of in window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose Please note that you need not now check window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() because it has been taken care of in window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose

You may verify this code when extension is enabled and disabled (Steps are given above).您可以在启用和禁用扩展时验证此代码(上面给出了步骤)。 This should work fine without any issues.这应该可以正常工作,没有任何问题。

All these code snippets are from the Redux devtools Readme file. 所有这些代码片段都来自 Redux devtools 自述文件。

These codes are for debugging on development/local environment only.这些代码仅用于在开发/本地环境中进行调试。 For production, you may add conditions to opt it out.对于生产,您可以添加条件以退出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法读取未定义的 react redux 的属性“apply” - Uncaught TypeError: Cannot read property 'apply' of undefined react redux 类型错误:无法读取未定义的属性“类型” - Redux - TypeError: Cannot read property 'type' of undefined - Redux TypeError:无法读取未定义的属性“ apply” - TypeError: Cannot read property 'apply' of undefined “TypeError:无法读取属性'apply'of undefined” - “TypeError: Cannot read property 'apply' of undefined” 类型错误:无法在 EventEmitter 读取未定义的属性“应用” - TypeError: Cannot read property 'apply' of undefined at EventEmitter 将redux-observable集成到现有项目中-Uncaught TypeError:无法读取未定义的属性“ apply” - Integrating redux-observable into existing project - Uncaught TypeError: Cannot read property 'apply' of undefined TypeError:无法在测试中使用javascript应用读取未定义的属性“ apply”? - TypeError: Cannot read property 'apply' of undefined, using javascript apply in tests? 反应 - Redux | 未处理的拒绝(类型错误):无法读取未定义的属性“数据” - React - Redux | Unhandled Rejection (TypeError): Cannot read property 'data' of undefined TypeError:无法读取未定义的属性“ map” React,Redux - TypeError: Cannot read property 'map' of undefined | React, Redux TypeError:无法读取Redux Todo应用程序中未定义的属性“ map” - TypeError: Cannot read property 'map' of undefined in Redux Todo application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM