onChange 函数 console.log 是一个字符短

IT技术 javascript reactjs
2021-05-04 02:36:23

我正在尝试在我的表单中创建密码确认功能。但是,在测试时,我的console.logs输出值总是短一个值,我不确定为什么会发生这种情况。

class SignUp extends React.Component {
    getInitialState() {
        return {
            initialPassword: "",
            confirmPassword: "",
            isMatchedPassword: true,
            isEmailInUse: false,
        }
    }


    checkAvailableEmail(event) {
        console.log("checkAvailableEmail");
        console.log(event.target.value);
    }

    confirmPassword(event) {
        console.log("confirmPassword");
        this.setState({
            confirmPassword: event.target.value
        })

        if (this.state.confirmPassword !== this.state.initialPassword) {
            console.log("Passwords DONT match");
            this.state.isMatchedPassword = false;
        } else {
            console.log("Passwords match");
            this.state.isMatchedPassword = true;
        }

        console.log("confirmPassword props");
        console.log(this.state);

    }

    setInitialPassword(event) {
        console.log("setInitialPassword");
        this.setState({
            initialPassword: event.target.value
        });
        console.log("setInitialPassword props");
        console.log(this.state);

    }

    render() {
        let hidden = "";
        Log.debug('Signup component props');
        Log.debug(this.props);

        return (
            <div>
                <form method="POST" action="/signup">
                    <ul>
                        <li><input id="firstName" name="first_name" placeholder="First name" type="text" required="required" /></li>
                        <li><input id="lastName" name="last_name" placeholder="Last name" type="text" required="required" /></li>
                        <li><input id="email" name="email" placeholder="Email" type="email" required="required" onBlur={ this.checkAvailableEmail }/></li>
                        <li><input id="password" name="password" placeholder="Password" type="password" required="required" onChange={ this.setInitialPassword}/></li>
                        <li><input id="confirmPassword" name="confirm_password" placeholder="Confirm Password" type="password" required="required" onChange={ this.confirmPassword } /></li>
                        <span id="confirmMessage" className={ hidden }>Password does not match</span>
                        <input type="submit" value="Sign up" />
                        <input type="reset" value="Cancel" />
                    </ul>
                </form>
            </div>
        );
    }
};

在此处输入图片说明

在此处输入图片说明

1个回答

重要的是要记住这setState是异步的

setState()不会立即变异,this.state而是创建一个挂起的状态转换。this.state调用此方法后访问可能会返回现有值。

第二个(可选)参数是一个回调函数,一旦setState完成并重新渲染组件就会执行

尝试更改confirmPassword为:

confirmPassword(event) {
    console.log("confirmPassword");
    this.setState({
        confirmPassword: event.target.value
    }, this.checkMatchingPasswords);

}

checkMatchingPasswords() {
    if (this.state.confirmPassword !== this.state.initialPassword) {
        console.log("Passwords DONT match");
        this.state.isMatchedPassword = false;
    } else {
        console.log("Passwords match");
        this.state.isMatchedPassword = true;
    }

    console.log("confirmPassword props");
    console.log(this.state);

}

不过里面的代码checkMatchingPasswords也是不对的:

切勿this.state直接变异,因为setState()之后调用可能会替换您所做的变异。将其this.state视为不可变的。

由于state.isMatchedPassword取决于state.initialPasswordand的值state.confirmedPassword,我会将其完全删除并将其转换为函数:

confirmPassword(event) {
    console.log("confirmPassword");
    this.setState({
        confirmPassword: event.target.value
    }, this.checkMatchingPasswords);

}

checkMatchingPasswords() {
    var { initialPassword, confirmedPassword } = this.state;
    if (this.isMatchedPassword(initialPassword, confirmedPassword)) {
        console.log("Passwords DONT match");
    } else {
        console.log("Passwords match");
    }
}

isMatchedPassword(initial, confirmed) {
    return initial === confirmed;
}

这是此技术的一个工作示例:https : //jsbin.com/huxuri/edit?js,output