简体   繁体   中英

Grabbing content from react input form when it is pre-filled by browser or password manager

I have a React app (with React Segment UI), which so far is working fine. I have a login form where, as any login form, I allow it to be auto-filled by either a password manager or the browser. What I would like to do is grab the values in the pre-filled forms, mainly to check if they have already been filled.

How can I do that?

This is currently what my component looks like:

export default class SignIn extends React.PureComponent {
  handleSubmit (evt) {
    // handle submit
  }

  isSignInDisabled () {
    // Would like to check here is the email and password
    // have been prefilled since with this method it won't work
    // in this case.
    return (
      !this.props.email.length ||
      !this.props.password.length
    )
  }

  render () {
    const { email, password } = this.props

    return (
        <Form onSubmit={evt => {
            this.handleSubmit(evt)
        }}>
          <Form.Field>
            <Form.Input
              type='email'
              label='Email'
              value={email}
              onChange={evt => setEmail(evt.target.value)}
            />
          </Form.Field>
            <Form.Field>
              <Form.Input
                type='password'
                label='Password'
                value={password}
                onChange={evt => setPassword(evt.target.value)}
              />
            </Form.Field>,
            <Form.Field>
              <Button type='submit' disabled={this.isSignInDisabled()}>Sign In</Button>
            </Form.Field>
          </Form>
    )
  }
}

To roughly grab the values from input fields you can use ref mechanism.

You should be careful : ref interacts with the real DOM, not the virtual React DOM.

Learn more about ref in the official docs .

 class Form extends React.Component { handleSubmit = (e) => { e.preventDefault(); console.log('Values are', this.firstName.value, this.lastName.value); } render() { return ( <form className="custom-form" onSubmit={this.handleSubmit}> <input type="text" name="first_name" value="John" ref={(input) => { this.firstName = input; }} /> <input type="text" name="last_name" value="Doe" ref={(input) => { this.lastName = input; }} /> <button type="submit">Submit</button> </form> ); } }; const App = () => ( <div> <Form /> </div> ); ReactDOM.render(<App />, document.getElementById('react')); 
 .custom-form { display: block; padding: 10px; } .custom-form > input { display: block; margin-top: 10px; padding: 5px 10px; border: 1px solid #ddd; border-radius: 3px; } .custom-form > button { display: block; margin-top: 10px; padding: 5px 10px; background: #fff; border-radius: 3px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="react"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM