javascript - onChange 函数 console.log 少了一个字符

标签 javascript reactjs

我正在尝试在我的表单中创建密码确认功能。然而,在测试时,我的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>
        );
    }
};

enter image description here

enter image description here

最佳答案

请务必记住 setState is asynchronous :

setState() does not immediately mutate this.state but creates a pending state transition. Accessing this.state after calling this method can potentially return the existing value.

The second (optional) parameter is a callback function that will be executed once setState is completed and the component is re-rendered.

尝试将 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 中的代码也不正确:

NEVER mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.

由于 state.isMatchedPassword 取决于 state.initialPasswordstate.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

关于javascript - onChange 函数 console.log 少了一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32674174/

相关文章:

javascript - ExtJs _dc 参数以 "/"斜杠开头

javascript - 在这里创建闭包的替代方案是什么?

javascript - 使用 Context API 与 CloneElement 传递直接后代的 Prop

reactjs - React Ant设计文件上传仅xls文件验证

javascript - 如何从字典中提取项目

javascript - 解析 URL 并在纯文本 Jade 上创建链接

javascript - 将对象列表 c# 传递给 javascript 时出错 : undefined variable

javascript - 在 Wordpress 页面中包含使用 Angular 构建的 html 页面

reactjs - NextJS 处理 "Server Error"和 "Client side error"的最佳实践

javascript - React-router v4 更新 url 但不渲染组件