简体   繁体   English

替代使用 react-redux 的调度钩子,用于 React 中的 Class 组件

[英]Alternative for useDispatch hook of react-redux for Class Component in React

react-redux library has come up with useDispatch , which being a hook can only be used in function component. react-redux库提供了useDispatch ,它是一个钩子,只能在 function 组件中使用。

import React from 'react';
import {useDispatch} from 'react-redux';
import {action} from './actions';

const funComp = () => {
  const dispatch = useDispatch();
  const performAction = () => { dispatch(action()) }
  return <button onClick={performAction}> Dispatch Action </button>
}

This simplifies a lot of boiler-plate code for functional component, is there something similar which we can use in class component?这简化了功能组件的许多样板代码,我们可以在 class 组件中使用类似的东西吗?

react-redux library came with latest useDispatch hook using which we can directly dispatch actions from a functional component react-redux库带有最新useDispatch钩子,我们可以使用它直接从功能组件中调度动作

react community as a whole seems to encourage this pattern of directly dispatching actions from dispatch variable instead of importing those dispatchers as props on the function component, with the help of mapDispatchToProps整个反应社区似乎鼓励这种直接从调度变量调度动作的模式,而不是在 mapDispatchToProps 的帮助下将这些调度程序作为mapDispatchToProps组件上的道具导入

We can achieve similar functionality for class component by importing the store and dispatching actions with its dispatch function, and it's no-brainer since useDispatch hook internally does the same thing.我们可以通过使用它的调度 function 导入存储和调度动作来为 class 组件实现类似的功能,因为 useDispatch 钩子在内部做同样的事情,这很no-brainer

so in a class component same can be achieved like this所以在 class 组件中,同样可以这样实现

An alternative to useDispatch hook for class component class 组件的 useDispatch hook 的替代方法

import React from "react";
import { incrementAction } from "./actions";
import store from "./store";

export class StoreFriend extends React.Component {
  handleStoreIncrement = () => {
    store.dispatch(incrementAction());    // <--- this is the trick. 
  };
  render() {
    return (
      <div>
        <button onClick={this.handleStoreIncrement}>store increment</button>
      </div>
    );
  }
}

Hacky Bonus: narrow use case alternative of useSelector for class component This is only applicable in case the component is aware when the value it requires from redux store is going to update Hacky Bonus:class 组件的 useSelector 的窄用例替代方案这仅适用于组件知道它需要从 redux 存储区更新的值时的情况

See the hacky solution below请参阅下面的 hacky 解决方案

export class StoreFriend extends React.PureComponent {
  
  handleStoreIncrement = () => {
    store.dispatch(incrementAction());
    this.forceUpdate(); // <---This is the hack, everytime class component dispatches the action, it should be forcefully re-rendered to get new value from store
  };
  render() {
    const s = store.getState().value;
    return (
      <div>
        <p>Comp shows store value --> {s}</p>
        <button onClick={this.handleStoreIncrement}>store increment</button>
      </div>
    );
  }
}

code sandbox link https://codesandbox.io/s/reactredux-qp05m?file=/Page.js代码沙箱链接https://codesandbox.io/s/reactredux-qp05m?file=/Page.js

Class components should use the React-Redux connect API to interact with the store: Class 组件应使用React-Redux connect API与商店交互:

import React from 'react';
import {useDispatch} from 'react-redux';
import {action} from './actions';

const mapState = state => {
  return {
    todos: state.todos
  }
};

const mapDispatch = {action};

class MyClassComponent extends React.Component {
  render() {
    const {todos} = this.props; // use to render something
    return <button onClick={this.props.action}> Dispatch Action </button>
  }
}

export default connect(mapState, mapDispatch)(MyClassComponent);

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

相关问题 有没有办法使用 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 React-Redux Class 组件 mapStateToProps 错误 - React-Redux Class Component mapStateToProps error 尝试导入错误:“useDispatch”未从“react-redux”导出 - Attempted import error: 'useDispatch' is not exported from 'react-redux' ReduxToolkit useSelector Hook:通过 useDispatch 更新选定的 redux-state 后,React 功能组件不会重新渲染 - ReduxToolkit useSelector Hook: React functional component doesnt rerender after selected redux-state is updated through useDispatch 组件未更新(react-redux) - Component is not updating (react-redux) React Redux - 混淆使用函数组件与 useDispatch 与类组件和 mapStateToProps - React Redux - Confusion on Using Functional Component with useDispatch vs Class Component and mapStateToProps 将 React-Redux useSelector Hook 作为道具传递 - Passing React-Redux useSelector Hook as Props 如何在基于类的组件(react-redux)上使用ownProps - How to use ownProps on a Class Based Component (react-redux) 如何在 class 组件中使用调度(react-redux) - How to use a dispatch (react-redux) into class Component 如何在 react-redux 组件上传递道具? - How to pass props on react-redux component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM