如何在React with Redux中正确管理组件?

时间:2018-07-06 07:31:17

标签: reactjs redux react-redux

这是我的登录页面容器组件:

  (
    "tier-1" = "3.9";
    "tier-2" = "4.9";
    "tier-3" = "5.9";
    free = 0;
  )

由于我只希望容器组件与redux状态进行通信,因此,我决定将value和callback函数都传递给SignUp组件,这样SignUp组件将只是无状态组件,不需要进行连接( )()还原。

这是我的SignUp子组件:

export class Loginpage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            toggleSignUp: false,
            displayName: '',
            signUpEmail: '',
            signUpPassword: ''
        };
    }

    //Sign up functions
    handleDisplayNameChange = (e) => {
        const displayName = e.target.value;
        this.setState(() => ({ displayName }));
    }

    handleSignUpEmailChange = (e) => {
        const signUpEmail = e.target.value;
        this.setState(() => ({ signUpEmail }));
    }

    handleSignUpPasswordChange = (e) => {
        const signUpPassword = e.target.value;
        this.setState(() => ({ signUpPassword }));
    }

    startSignUp = () => {
        this.props.startSignUp(this.state.signUpEmail, this.state.signUpPassword);
    }

    render() {
        return (
            <div className="login-container">
                <SignUp
                    toggleSignUp={this.toggleSignUp}
                    displayName={this.state.displayName}
                    signUpEmail={this.state.signUpEmail}
                    signUpPassword={this.state.signUpPassword}
                    handleDisplayNameChange={this.handleDisplayNameChange}
                    handleSignUpEmailChange={this.handleSignUpEmailChange}
                    handleSignUpPasswordChange={this.handleSignUpPasswordChange}
                    startSignUp={this.startSignUp}
                    />
            </div>
        )
    }
}

代码工作正常,我只是不确定这是否是管理React组件结构的正确方法?我非常怀疑使用道具将值和回调函数传递给子组件是否正确。

0 个答案:

没有答案