1

I am new to react and i think it is a silly question to ask but then too I have a generic input component of text field in which i haven't passed refs and i want to reset the value of text field on button click? Is it possible to clear the value of field on click without passing refs?

handleClick : function(change){
change.preventDefault();
var self=this;
if(this.state.newpwd === this.state.cfmpass){
  var pass = {"pwd":self.state.newpwd};
  var url = "xyz"
  Request.PatchRequest(url,pass,function(response){
    self.setState({
      authMsg : JSON.parse(response.response).data
    });

now how to clear the field value here??

<TextBox type="password"
                name="password"
                placeholder="Enter new password"
                onChange={this.changePwd} />

this is my button on which i want to perform check (which i have done) and after response i want to clear the field value

<Button type="button"
        value="Change"
        onClick={this.handleClick}/>

TextBox is my generic component.. Any help will be very thankful. Thank you in advance :)

2 Answers 2

0

You will need a component's prop that 'control' the input's value with onChange function. You'll need to do some steps:

  • Add inside your constructor method the input's value initial state, most cases uses a empty string
  • Make your onChange function set a new state for that value
  • Do not forget to bind your function inside constructor

For even more details, you can copy and paste your components code, to confirm your react syntax (es5, es6 or es7)

Example:

constructor() {

 this.state: {
   value: ''

this.changePwd = this.changePwd.bind(this)
}

changePwd(event) {
  this.setState({ value: event.target.value })
}

<TextBox 
  type="password"
  name="password"
  placeholder="Enter new password"
  value={this.state.value}
  onChange={this.changePwd} 
/>
Sign up to request clarification or add additional context in comments.

Comments

0

This approach assumes the parent component (the thing that holds both the textarea and the button) is maintaining the state of the textarea (which in this case, would be the way to do it)

// somewhere within the component (probably the container component)
reset() {
    this.setState({ value: '' });
}

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

// render
        <TextBox 
            type="password"
            name="password"
            placeholder="Enter new password"
            onChange={this.onChange} 
            value={this.state.value}
        />
        <button onClick={this.reset}>reset</button>

EDIT Updated based on an updated question...

constructor(props) {
    super(props);
    this.change = this.change.bind(this);
    this.inputOnChange = this.inputOnChange.bind(this);
}

change(e) {
    e.preventDefault();

    if(this.state.newpwd === this.state.cfmpass) {
        var pass = {
            "pwd": this.state.newpwd
        };
        var url = "xyz";

        Request.PatchRequest(url, pass, (response) => {
            this.setState({
                authMsg : JSON.parse(response.response).data,
                value: '', 
            });
        });
}

inputOnChange(e) {
    this.setState({ value: e.target.value });
}

// render
render() {
    return (    
        <TextBox 
            type="password"
            name="password"
            placeholder="Enter new password"
            onChange={this.inputOnChange} 
            value={this.state.value}
        />
        <button onClick={this.change}>Change</button>
    );
}

5 Comments

yeah got that.. but on your reset button i want to confirm that password and current password are matching or not.. and after that if it matches i will display the response of my post request and after that i want to clear the input field value
Got that.. in the sense that this answers your question?
You mention you want the field cleared after a response, what response?
check the question again i updated it for better understanding.. by response i mean api response
Updated based on your new question

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.