简体   繁体   English

如何在 class 组件中使用调度(react-redux)

[英]How to use a dispatch (react-redux) into class Component

My code我的代码

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM