简体   繁体   English

有没有办法使用 react-redux (useDispatch) 使用基于 class 的组件进行反应? 我在渲染 function 中使用 useDispatch

[英]Is there a way to use react-redux (useDispatch) in react using class based components? I am using useDispatch inside render function

Error: Invalid hook call.错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app你可能在同一个应用程序中拥有多个 React 副本

Hooks are intended to be used in Functional components only.挂钩仅用于功能组件。 As per the Rules of hooks they can be called from根据钩子规则,它们可以从

  • React function components.反应 function 组件。
  • Custom Hooks自定义挂钩

In order to other question为了其他问题

Is there a way to use react-redux (useDispatch) in react using class based components?有没有办法使用 react-redux (useDispatch) 使用基于 class 的组件进行反应?

Yes, there are ways of using react-redux inside class based components.是的,有一些方法可以在基于 class 的组件中使用 react-redux。 Using connect API provided by react-redux library which will inject the action creators into the class component.使用react-redux库提供的连接API 将动作创建者注入 class 组件。 Eventually accessing the required dispatch function by this.props .最终通过 this.props 访问所需的dispatch this.props You can have a look here for a basic example of how it can be used.您可以在这里查看如何使用它的基本示例。

Hope this helps.希望这可以帮助。

You cannot use hooks with class components.您不能将挂钩与 class 组件一起使用。 As written in the docs , you need to create a container/wrapper component that passes the actions.文档中所写,您需要创建一个传递操作的容器/包装器组件。

For that you have to use connect from 'react-redux' like this:为此,您必须像这样使用“react-redux”中的connect

import { connect } from 'react-redux'

const mapDispatchToProps = dispatch => {
  return {
    onTodoClick: id => {
      dispatch(toggleTodo(id))
    }
  }
}

const VisibleTodoList = connect(mapStateToProps (HERE NOT NEEDED; IF YOU WANT TO DISPATCH ONLY), mapDispatchToProps)(TodoList)

export default VisibleTodoList

This would pass onTodoClick to your component via props which you can just call like any prop: this.props.onTodoClick()这将通过道具将onTodoClick传递给您的组件,您可以像任何道具一样调用它: this.props.onTodoClick()

暂无
暂无

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

相关问题 我正在尝试在我的 react redux 应用程序中使用 map。 但使用useDispatch后,控制台显示错误 - I am trying to use map in my react redux app. but after using useDispatch, the console is showing error 替代使用 react-redux 的调度钩子,用于 React 中的 Class 组件 - Alternative for useDispatch hook of react-redux for Class Component in React 如何使用thunk和useDispatch(react-redux hooks)从动作返回一个promise? - How to return a promise from an action using thunk and useDispatch (react-redux hooks)? 我在使用 React Hook 时遇到了问题 - useDispatch() - I got the problem in using React Hook - useDispatch() react redux 为什么使用dispatch = useDispatch()? - react redux why use dispatch = useDispatch()? 尝试导入错误:“useDispatch”未从“react-redux”导出 - Attempted import error: 'useDispatch' is not exported from 'react-redux' React Redux - 混淆使用函数组件与 useDispatch 与类组件和 mapStateToProps - React Redux - Confusion on Using Functional Component with useDispatch vs Class Component and mapStateToProps 反应-Redux。 通过 useDispatch 更改状态后,组件未通过 useSelector 更新 - React-Redux. After changing state by useDispatch, components not updating by useSelector 反应 Redux state 在调用 useDispatch() 后没有改变 - React Redux state not changing after calling useDispatch() React redux - 映射对象时 useSelect() 和 useDispatch() - React redux - useSelect() and useDispatch() when mapping objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM