[英]How to use a dispatch (react-redux) into class Component
class LoginPage extends Component {
render() {
return (
<div>
<TextInput/>
<br/>
<TextInput/>
<br/>
<Button onClick={() => {
const dispatcher = useDispatch();
dispatcher(singIn())
}}>SING IN</Button>
</div>
);
}
}
I guess that I am using a hooks in a class component, but what can I use instead of useDispacth
to my LoginPage?我想我在 class 组件中使用了一个钩子,但是我可以使用什么来代替我的 LoginPage 的
useDispacth
?
For class components, you need to use connect
method from react-redux
.对于 class 组件,您需要使用
react-redux
中的connect
方法。 Use connect method and pass mapDispatchToProps
function as shown below.使用 connect 方法并通过
mapDispatchToProps
function 如下所示。
import { connect } from 'react-redux';
class LoginPage extends Component {
render() {
return (
<div>
<TextInput/>
<br/>
<TextInput/>
<br/>
<Button onClick={() => {
this.props.singIn()
}}>SING IN</Button>
</div>
);
}
}
const mapDispatchToProps = (dispatch) => {
return {
signIn: () => dispatch(signIn())
}
};
export default connect(null, mapDispatchToProps)(LoginPage)
Read thru the doc for more info.阅读文档以获取更多信息。 https://react-redux.js.org/api/connect
https://react-redux.js.org/api/connect
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.