`useSelector`在运行应用程序时未定义,来自于’react-redux’。

huangapple go评论189阅读模式
英文:

useSelector from 'react-redux' is undefined when running application

问题

以下是翻译好的部分:

"react-redux" 中的 "useSelector" 函数在我的 React 应用中是 "undefined" 的,尽管版本是最新的,而且在 VS Code 中没有错误(我甚至可以使用 ctrl + 单击查看类型,并查看 node_modules 文件夹中的代码)。这以几种方式表现出来:

  1. 以下错误:[HMR] bundle has 1 warnings: export 'useSelector' was not found in 'react-redux'

  2. 在以下组件中:

    1. import React, { useState } from 'react';
    2. import { useSelector } from 'react-redux'; // 导入 useSelector 在 VS Code 中不会出错
    3. const TestComponent = () => {
    4. const [x, setX] = useState("hello"); // useState 正常工作
    5. console.log(useSelector); // 但当应用运行时,useSelector 为 undefined
    6. return <p> {x} </p>;
    7. }
    8. export default TestComponent;

我不确定这个错误是否与热重载、webpack 或其他原因有关。

包版本:

  1. // react
  2. "@types/react": "^16.9.9",
  3. "react": "^16.10.2",
  4. // redux
  5. "@types/react-redux": "^7.1.5",
  6. "react-redux": "^7.1.3",
  7. "redux": "^4.0.5",

我尝试过的方法:

  1. 更新所有 redux 包到最新版本

  2. 删除 node_modules 并重新安装

  3. 更新热重载包到最新版本

  4. 查看 node_modules 中的 react-redux 代码,它确实具有 useSelector 钩子,但如果我执行以下操作:import * as ReactRedux from 'react-redux'; 然后 console.log(ReactRedux),我会看到以下内容,表示没有导入任何钩子:

`useSelector`在运行应用程序时未定义,来自于’react-redux’。
1: https://i.stack.imgur.com/YGWTC.png

英文:

The useSelector function from &#39;react-redux&#39; is undefined in my react application, even though the versions are up to date, and there are no errors in VS Code (I can even ctrl-click in and see the typings, and the code in the node_modules folder). This manifests in a few ways:

  1. The following error [HMR] bundle has 1 warnings: export &#39;useSelector&#39; was not found in &#39;react-redux&#39;.

  2. In the following component:

    import React, { useState } from 'react';
    import { useSelector } from 'react-redux'; // importing useSelector does not error in VS Code

    const TestComponent = () => {
    const [x, setX] = useState("hello"); // useState works correctly
    console.log(useSelector); // but useSelector is undefined when the application runs
    return <p> {x} </p>
    }

    export default TestComponent;

I am not sure if this error is to do with hot reloading, webpack, or something else entirely.

Package versions:

  1. // react
  2. &quot;@types/react&quot;: &quot;^16.9.9&quot;,
  3. &quot;react&quot;: &quot;^16.10.2&quot;,
  4. // redux
  5. &quot;@types/react-redux&quot;: &quot;^7.1.5&quot;
  6. &quot;react-redux&quot;: &quot;^7.1.3&quot;,
  7. &quot;redux&quot;: &quot;^4.0.5&quot;,

What I have attempted:

  1. Updated all redux packages to latest versions

  2. Deleted node_modules and reinstalled

  3. Updated hot reloading packages to latest versions

  4. Looking into the code of react-redux in node_modules, it does have the useSelector hook, but if I do the following: import * as ReactRedux from &#39;react-redux&#39;; and then console.log(ReactRedux), I see the following, indicating none of the hooks have been imported:

`useSelector`在运行应用程序时未定义,来自于’react-redux’。
1: https://i.stack.imgur.com/YGWTC.png

答案1

得分: 2

在你的代码片段中,没有找到任何使用的存储库。

我已经添加了一个示例工作应用程序,该应用程序增加和减少值,以及登录和登出。

请查看下面的代码片段:

  1. import React, { useEffect } from "react";
  2. import ReactDOM from "react-dom";
  3. import { useSelector, useDispatch, Provider } from "react-redux";
  4. import allActions from "./actions";
  5. import { createStore } from "redux";
  6. import rootReducer from "./reducers";
  7. const store = createStore(
  8. rootReducer,
  9. window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  10. );
  11. const App = () => {
  12. const counter = useSelector(state => state.counter);
  13. const currentUser = useSelector(state => state.currentUser);
  14. const dispatch = useDispatch();
  15. const user = { name: "Rei" };
  16. useEffect(() => {
  17. dispatch(allActions.userActions.setUser(user));
  18. }, []);
  19. return (
  20. <div className="App">
  21. {currentUser.loggedIn ? (
  22. <>
  23. <h1>Hello, {currentUser.user.name}</h1>
  24. <button onClick={() => dispatch(allActions.userActions.logOut())}>
  25. Logout
  26. </button>
  27. </>
  28. ) : (
  29. <>
  30. <h1>Login</h1>
  31. <button
  32. onClick={() => dispatch(allActions.userActions.setUser(user))}
  33. >
  34. Login as Rei
  35. </button>
  36. </>
  37. )}
  38. <h1>Counter: {counter}</h1>
  39. <button onClick={() => dispatch(allActions.counterActions.increment())}>
  40. Increase Counter
  41. </button>
  42. <button onClick={() => dispatch(allActions.counterActions.decrement())}>
  43. Decrease Counter
  44. </button>
  45. </div>
  46. );
  47. };
  48. const rootElement = document.getElementById("root");
  49. ReactDOM.render(
  50. <Provider store={store}>
  51. <App />
  52. </Provider>,
  53. rootElement
  54. );

工作示例:codesandbox链接

英文:

So in your codesnippet, didnt find any store which is used.

I have added a sample working application which increments and decrements the value and sign in and signout

See the below snippet and check

App.js

  1. import React, { useEffect } from &quot;react&quot;;
  2. import ReactDOM from &quot;react-dom&quot;;
  3. import { useSelector, useDispatch, Provider } from &quot;react-redux&quot;;
  4. import allActions from &quot;./actions&quot;;
  5. import { createStore } from &quot;redux&quot;;
  6. import rootReducer from &quot;./reducers&quot;;
  7. const store = createStore(
  8. rootReducer,
  9. window.__REDUX_DEVTOOLS_EXTENSION__ &amp;&amp; window.__REDUX_DEVTOOLS_EXTENSION__()
  10. );
  11. const App = () =&gt; {
  12. const counter = useSelector(state =&gt; state.counter);
  13. const currentUser = useSelector(state =&gt; state.currentUser);
  14. const dispatch = useDispatch();
  15. const user = { name: &quot;Rei&quot; };
  16. useEffect(() =&gt; {
  17. dispatch(allActions.userActions.setUser(user));
  18. }, []);
  19. return (
  20. &lt;div className=&quot;App&quot;&gt;
  21. {currentUser.loggedIn ? (
  22. &lt;&gt;
  23. &lt;h1&gt;Hello, {currentUser.user.name}&lt;/h1&gt;
  24. &lt;button onClick={() =&gt; dispatch(allActions.userActions.logOut())}&gt;
  25. Logout
  26. &lt;/button&gt;
  27. &lt;/&gt;
  28. ) : (
  29. &lt;&gt;
  30. &lt;h1&gt;Login&lt;/h1&gt;
  31. &lt;button
  32. onClick={() =&gt; dispatch(allActions.userActions.setUser(user))}
  33. &gt;
  34. Login as Rei
  35. &lt;/button&gt;
  36. &lt;/&gt;
  37. )}
  38. &lt;h1&gt;Counter: {counter}&lt;/h1&gt;
  39. &lt;button onClick={() =&gt; dispatch(allActions.counterActions.increment())}&gt;
  40. Increase Counter
  41. &lt;/button&gt;
  42. &lt;button onClick={() =&gt; dispatch(allActions.counterActions.decrement())}&gt;
  43. Decrease Counter
  44. &lt;/button&gt;
  45. &lt;/div&gt;
  46. );
  47. };
  48. const rootElement = document.getElementById(&quot;root&quot;);
  49. ReactDOM.render(
  50. &lt;Provider store={store}&gt;
  51. &lt;App /&gt;
  52. &lt;/Provider&gt;,
  53. rootElement
  54. );

Working codesandbox

答案2

得分: -2

The workaround for me was to install metro package version 0.56.0 .

Via npm:
npm install metro@0.56.0

Via yarn
yarn add metro@0.56.0

英文:

The workaround for me was to install metro package version 0.56.0 .

Via npm:
npm install metro@0.56.0

Via yarn
yarn add metro@0.56.0

huangapple
  • 本文由 发表于 2020年1月3日 21:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定