7

I'm using autocomplete of material-ui, and trying to delete selected value whenever click a button but can not find any way to do that. Any idea?

            <Autocomplete
              className={classes.techListBox}
              disableCloseOnSelect={true}
              multiple
              options={this.props.displayProject.techList}
              getOptionLabel={options => options.title}
              defaultValue={this.props.displayProject.techName}
              onChange={(e, techs) => {
                this.formatTechID(techs);
              }}
              renderInput={params => (
                <TextField
                  {...params}
                  variant="outlined"
                  placeholder={t("tech")}
                  margin="normal"
                  fullWidth
                />
              )}
            ></Autocomplete>```

1 Answer 1

6

You will need to set the value(a state) and the onChange event at Autocomplete:) when you click the rest button it will just reset the state :)

   const [value, setValue] = React.useState(null);
   <Autocomplete 
    value={value}
    onChange={(event, newValue) => {
      setValue(newValue);
    }}
   >

   <button onClick={() => setValue(null)}>Reset autocomplete</button>

I made a working demo for you: https://codesandbox.io/s/material-demo-zqz4v

Comment down for more questions :)

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think this work when select multiple. I stored select value, and i can set store to null, but can not remove it in screen. How can I do that. And sorry for my English
It will work for multiple too :) but now you will array and not a basic variable. Click in the link I provided FULL DEMO , you have the example there where it is removed ;) Feel free to UPVOTE + accept the answer

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.