0

I am trying to create a password confirmation checker in reactJS. I am currently testing out firing the callbacks within the component. When I test this view, I dont see my onChange firing off any callbacks. I am pretty new to reactjs so I'm not sure why nothing is happening.

import React from 'react';

class SignUp extends React.Component {

    confirmPassword(event) {
        console.log("confirmPassword");
        console.log(event);
    }

    setInitialPassword(event) {
        console.log("setInitialPassword");
        console.log(event);
    }

    render() {
        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" /></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" class="confirm-message"></span>
                        <input type="submit" value="Sign up" />
                        <input type="reset" value="Cancel" />
                    </ul>
                </form>
            </div>
        );
    }
};

export default SignUp;
3
  • Do you have any errors? It seems to work correctly in jsfiddle: jsfiddle.net/g5gtfbcv/1 Commented Sep 2, 2015 at 15:31
  • Its works with this fiddle : jsfiddle.net/69z2wepo/15118. By the way you should write className instead of class. Commented Sep 2, 2015 at 15:31
  • If you use React.createClass instead of the ES6 class syntax, you get autobinding and this will Just Work™. Commented Sep 2, 2015 at 16:54

1 Answer 1

1

You need to bind this when you set your handlers.

<li><input id="password" name="password" placeholder="Password" type="password" required="required" onChange={ this.setInitialPassword.bind(this) }/></li>
<li><input id="confirmPassword" name="confirm_password" placeholder="Confirm Password" type="password" required="required" onChange={ this.confirmPassword.bind(this) } /></li>

or using ES6 big arrow functions:

<li><input id="password" name="password" placeholder="Password" type="password" required="required" onChange={ e => this.setInitialPassword(e) }/></li>
<li><input id="confirmPassword" name="confirm_password" placeholder="Confirm Password" type="password" required="required" onChange={ e => this.confirmPassword(e) } /></li>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.