【问题标题】:Failed to compile 'dispatch' is not defined编译失败 'dispatch' 未定义
【发布时间】:2021-08-10 17:45:03
【问题描述】:

我正在编写一个计数器,但出现这样的错误:

11:29 行:'dispatch' 未定义 no-undef

这是我的代码:

import React from "react";
import {useSelector,useDispatch} from 'react-redux';
import {increment} from './actions'

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

  return (
    <div>
      <h1>Counter {counter}</h1>
      <button onClick={()=> dispatch(increment())}>+</button> //line:11
      <button>-</button>
      {isLogged ? <h3>Value info can't be display</h3>: '' }
      
    </div>
  );
}

export default App;

正如理解的增量是一个动作。你能帮我吗:)?

【问题讨论】:

    标签: javascript reactjs redux react-redux react-hooks


    【解决方案1】:

    您从react-redux 导入了useDispatch,但没有使用它。

    import React from "react";
    import {useSelector,useDispatch} from 'react-redux';
    import {increment} from './actions'
    
    function App() {
      const dispatch = useDispatch(); // Use it
      const counter = useSelector(state => state.counter);
      const isLogged = useSelector(state => state.isLogged);
    
      return (
        <div>
          <h1>Counter {counter}</h1>
          <button onClick={()=> dispatch(increment())}>+</button> //line:11
          <button>-</button>
          {isLogged ? <h3>Value info can't be display</h3>: '' }
          
        </div>
      );
    }
    
    export default App;
    

    【讨论】:

      【解决方案2】:

      dispatch 确实没有定义。您可能必须执行以下操作:

      const dispatch = useDispatch();
      

      【讨论】:

        【解决方案3】:

        我修正了我的错误感谢:

        蒂亚戈·洛迪,蒂格兰·亚伯拉罕扬

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多