【发布时间】:2020-02-12 04:07:13
【问题描述】:
我正在尝试在组件的渲染函数中使用useDispatch。
import { useDispatch } from 'react-redux';
我有一个尝试在onChange 上使用Dispatch 的输入
onChange={() => useDispatch()(update("test"))}
当 onChange 被触发时,我得到这个错误:
无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一:
- 您的 React 和渲染器版本可能不匹配(例如 React DOM)
- 您可能违反了 Hooks 规则
- 您可能在同一个应用中拥有多个 React 副本
import React, {
Component
} from 'react';
import {
useDispatch
} from 'react-redux';
import {
update
} from '../store/actions';
class Something extends Component {
render() {
return (<input onChange={() => useDispatch()(update("test"))}/>)
}
}
export default Something;
【问题讨论】:
-
useDispatch是您构建的自定义挂钩还是来自任何其他库? -
来自 react-redux。
-
@T.J.Crowder 谢谢。我希望我能在我的日常工作中使用这个很棒的 React.. :( 无论如何很高兴知道.. 我现在正在研究它。
-
Hooks can only be called inside of the body of a function component.
标签: reactjs typescript redux react-redux