Reset React-Select (Unform) after Ubmit

Asked

Viewed 626 times

-4

I’m studying Reactjs and Typescript, and also a lib for forms called Unform (https://github.com/Rocketseat/unform).

I have a form with some fields, among them a Select field (https://react-select.com/). My problem is: How to clear this React-select field after submitting the form?

Repository of my project: https://github.com/fredarend/icetec-frontend

The form is in: src/pages/Dashboard/indext.tsx and the Select component is in src/Components/Select/index.tsx.

Note that in the Dashboard index I’ve used the formRef.current?.reset() after sending the data to the API, however, it is resetting only the inputs, Select does not continue with filled.

I’m not getting to implement anything that works, someone might have done it already?

From now on, thank you very much for your help!

  • value={selectedOption} in that part of the code you arrow selectedOption = null usually with component status variable.

2 answers

1


0

Add these lines to registerField of its React-select component.

clearValue: (ref: any) => {
  ref.select.clearValue();
}

When the clear is called, either by:

formRef.current.clearField('field-name'); or

formRef.current.reset(); or

function handleSubmit(data, { reset }) {
    reset();
}

the clearValue will be called and the ref.select.clearValue() will clear the field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.