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

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

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. 在以下组件中:

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

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

包版本:

// react
"@types/react": "^16.9.9",
"react": "^16.10.2",

// redux
"@types/react-redux": "^7.1.5",
"react-redux": "^7.1.3",
"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:

// react
&quot;@types/react&quot;: &quot;^16.9.9&quot;,
&quot;react&quot;: &quot;^16.10.2&quot;,

// redux
&quot;@types/react-redux&quot;: &quot;^7.1.5&quot;
&quot;react-redux&quot;: &quot;^7.1.3&quot;,
&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

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

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

请查看下面的代码片段:

import React, { useEffect } from "react";
import ReactDOM from "react-dom";
import { useSelector, useDispatch, Provider } from "react-redux";
import allActions from "./actions";
import { createStore } from "redux";
import rootReducer from "./reducers";

const store = createStore(
  rootReducer,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);

const App = () => {
  const counter = useSelector(state => state.counter);
  const currentUser = useSelector(state => state.currentUser);

  const dispatch = useDispatch();

  const user = { name: "Rei" };

  useEffect(() => {
    dispatch(allActions.userActions.setUser(user));
  }, []);

  return (
    <div className="App">
      {currentUser.loggedIn ? (
        <>
          <h1>Hello, {currentUser.user.name}</h1>
          <button onClick={() => dispatch(allActions.userActions.logOut())}>
            Logout
          </button>
        </>
      ) : (
        <>
          <h1>Login</h1>
          <button
            onClick={() => dispatch(allActions.userActions.setUser(user))}
          >
            Login as Rei
          </button>
        </>
      )}
      <h1>Counter: {counter}</h1>
      <button onClick={() => dispatch(allActions.counterActions.increment())}>
        Increase Counter
      </button>
      <button onClick={() => dispatch(allActions.counterActions.decrement())}>
        Decrease Counter
      </button>
    </div>
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  rootElement
);

工作示例: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

import React, { useEffect } from &quot;react&quot;;
import ReactDOM from &quot;react-dom&quot;;
import { useSelector, useDispatch, Provider } from &quot;react-redux&quot;;
import allActions from &quot;./actions&quot;;
import { createStore } from &quot;redux&quot;;
import rootReducer from &quot;./reducers&quot;;
const store = createStore(
rootReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ &amp;&amp; window.__REDUX_DEVTOOLS_EXTENSION__()
);
const App = () =&gt; {
const counter = useSelector(state =&gt; state.counter);
const currentUser = useSelector(state =&gt; state.currentUser);
const dispatch = useDispatch();
const user = { name: &quot;Rei&quot; };
useEffect(() =&gt; {
dispatch(allActions.userActions.setUser(user));
}, []);
return (
&lt;div className=&quot;App&quot;&gt;
{currentUser.loggedIn ? (
&lt;&gt;
&lt;h1&gt;Hello, {currentUser.user.name}&lt;/h1&gt;
&lt;button onClick={() =&gt; dispatch(allActions.userActions.logOut())}&gt;
Logout
&lt;/button&gt;
&lt;/&gt;
) : (
&lt;&gt;
&lt;h1&gt;Login&lt;/h1&gt;
&lt;button
onClick={() =&gt; dispatch(allActions.userActions.setUser(user))}
&gt;
Login as Rei
&lt;/button&gt;
&lt;/&gt;
)}
&lt;h1&gt;Counter: {counter}&lt;/h1&gt;
&lt;button onClick={() =&gt; dispatch(allActions.counterActions.increment())}&gt;
Increase Counter
&lt;/button&gt;
&lt;button onClick={() =&gt; dispatch(allActions.counterActions.decrement())}&gt;
Decrease Counter
&lt;/button&gt;
&lt;/div&gt;
);
};
const rootElement = document.getElementById(&quot;root&quot;);
ReactDOM.render(
&lt;Provider store={store}&gt;
&lt;App /&gt;
&lt;/Provider&gt;,
rootElement
);

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:

确定