Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear the material UI text field Value in react

How to clear the materialUI textfield value in react?

Check the below code -

<TextField
  hintText=""
  ref={(node) => this._toField = node}
  onChange={this.changeToText}
  floatingLabelText="To*"
  floatingLabelFixed={true}
  fullWidth={true}
/>

I'm using the raisedButton while pressing it validate the above field. If the field has error then displaying the error message. If not, then we need to clear the input. But how can we clear the input text?

like image 916
Shubham Avatar asked Feb 05 '18 07:02

Shubham


1 Answers

if you are using a stateless functional component then you can use react hooks.

Also make sure you are using inputRef

import React, { useState, useRef } from "react";

let MyFunctional = props => {
  let textInput = useRef(null);

  return (
    <div>
      <Button
        onClick={() => {
          setTimeout(() => {
            textInput.current.value = "";
          }, 100);
        }}
      >
        Focus TextField
      </Button>
      <TextField
        fullWidth
        required
        inputRef={textInput}
        name="firstName"
        type="text"
        placeholder="Enter Your First Name"
        label="First Name"
      />
    </div>
  );
};
like image 175
44kksharma Avatar answered Sep 23 '22 07:09

44kksharma