Gambit2007
Gambit2007

Reputation: 3968

Can't type in an input field

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?

Upvotes: 2

Views: 105

Answers (2)

ProblemSolver
ProblemSolver

Reputation: 644

Or rather U can have ur onChange like this:

    onChange(e) {
    this.setState(
        {password: e.target.value})
}

Upvotes: 0

Pavan
Pavan

Reputation: 1015

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 }
/>

Upvotes: 5

Related Questions