[英]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.