2

I have the following code:

    constructor(props) {
        super(props);
        this.state = {
            search: "",
            value: "",
            username:'',
            email:'',
            password: '',
        };

        this.onChange = this.onChange.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
    }

...
...

    onChange(e) {
        this.setState(
            {[e.target.name]: e.target.value})
    }

...
...

<input type="password" placeholder="Password"
onChange={e => this.onChange(e)}
value={ this.state.password }
                                                />

But I can't type into the password field. If I remove the value={ this.state.password } part I can then type into the field, but it seems like my state isn't being updated when this field is changed.

What is the problem?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Gambit2007
  • 3,260
  • 13
  • 46
  • 86

2 Answers2

5

You forgot to give name to the password field. It should be

<input 
 type="password" 
 placeholder="Password"
 name="password"
 onChange={e => this.onChange(e)}
 value={ this.state.password }
/>
Pavan
  • 985
  • 2
  • 15
  • 27
0

Or rather U can have ur onChange like this:

    onChange(e) {
    this.setState(
        {password: e.target.value})
}
ProblemSolver
  • 636
  • 1
  • 8
  • 16