3

I am using a template that build on material ui. I try to put onChange in this code but it doesnt work. I also try to add onChange in customInput too but still doesnt work. I am think to put onChang inside inputProps but I have no idea how to put function in object.

handleChange = e => {
    const {name, value} = e.currentTarget;
    this.setState({[name]: value});
};
......
<CustomInput
                             labelText="Username"
                             id="username"
                             name='username'
                             **onChange = {e =>this.handleChange(e)}
                             formControlProps={{
                               fullWidth: true
                             }}
                             inputProps={{                              
                              value: `${this.state.user.username}`, 
                            }}
                           />




How can I fix this problem?Thanks

2
  • remove ** before onChange also do you receive const {name, value}? Commented Oct 23, 2019 at 9:36
  • post the example of the question with modification in this template: codesandbox.io/s/49t79 and repost new url. Commented Oct 23, 2019 at 9:38

1 Answer 1

3

A sample on how to use onChange:

<CustomInput
    id="pass"
    formControlProps={{
        fullWidth: true
    }}
    inputProps={{
        onChange: (event) => this.handleChange(event),
        placeholder: "Password",
        type: "password"
    }}
 />

Here is the source of the sample, as well as a discussion on how to use the onChange event.

Finally, assuming that all the rest of your code works, this is (kind of) what you need:

handleChange = e => {
    const {name, value} = e.currentTarget;
    this.setState({[name]: value});
};
......
<CustomInput
    labelText="Username"
    id="username"
    name='username'
    formControlProps={{
        fullWidth: true
    }}
    inputProps={{   
        onChange: (e) => this.handleChange(e),                           
        defaultValue: `${this.state.user.username}`
    }}
/>
Sign up to request clarification or add additional context in comments.

2 Comments

I try and it doesnt work at first, but it does when I change value to defaultValue. Here is my final code <CustomInput inputProps={{ defaultValue:${this.state.user.username}, name:"username", onChange: (e) => this.handleChange(e), }} />
Great! Thanks :)

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.