2

i Used material Design For my login From but when i get console.log in onsubmitForm show ref value undefined... In the following simple example, both this.UserEmail.value & this.UserPassword.value are undefined when the submit is clicked.

  export default class SignIn extends React.Component {
      UserEmail= null;
      UserPassword=null;

      onsubmitForm = (e) =>
      {
        e.preventDefault();
        let EmailData = this.UserEmail.value;
        let PassData = this.UserPassword.value;
        let data = {EmailData,PassData}
        console.log("this is the submit form function");
        console.log(data);  // I Get Data Obj Undefined
      }

      render(){
      return (
         <div>
            <form>
              <TextField
                variant="outlined"
                margin="normal"
                required
                ref={el => this.UserEmail = el}
                id="email"
                label="Email"
                name="email"
                autoComplete="email"
                autoFocus
              />
              <TextField
                variant="outlined"
                margin="normal"
                fullWidth
                ref={el => this.UserPassword = el}
                name="password"
                label="Password"
                type="password"
                id="password"
                autoComplete="current-password"
              />

              <Button
                type="submit"
                onClick={this.onsubmitForm}
                variant="contained"
                color="primary"
              >
                Login
              </Button>
            </form>
          </div>
      );
    }
    }
1
  • 2
    Please take the time to create a working example. You can use codesandbox.io for that. Commented May 3, 2020 at 21:17

1 Answer 1

4

The value of the ref is undefined because for Material-UI's API requires that you use inputRef instead of ref to access the input elements inside the nested components.

export default class SignIn extends React.Component {
  onsubmitForm = e => {
    e.preventDefault();
    const email = this.email.value;
    const password = this.password.value;

    let data = { email, password };
    console.log("this is the submit form function");
    console.log(data);
  };

  render() {
    return (
      <div>
        <form>
          <TextField
            variant="outlined"
            margin="normal"
            required
            inputRef={email => (this.email = email)}
            id="email"
            label="Email"
            name="email"
            autoComplete="email"
            autoFocus
          />
          <TextField
            variant="outlined"
            margin="normal"
            fullWidth
            inputRef={password => (this.password = password)}
            name="password"
            label="Password"
            type="password"
            id="password"
            autoComplete="current-password"
          />
          <Button
            type="submit"
            onClick={this.onsubmitForm}
            variant="contained"
            color="primary"
          >
            Login
          </Button>
        </form>
      </div>
    );
  }
}

Working example here

Sign up to request clarification or add additional context in comments.

2 Comments

The refs in OPs code are being created correctly. As I found out the OP is not using basic HTML elements, the component element is not the input field itself but a wrapping div for Material UI's TextField component
Thanks for the clarification Milton. You're right <TextField> is indeed a wrapper component, but the input refs can be accessed using inputRef.

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.