Changing prop value with onError when MUI DateTimePicker error=true

Viewed 36

I'm trying to change 'myError' to true when MUI DateTimePicker throws onError but it doesn't work. The whole code itself is correct because when I change

() => {setValue({
              ...value,
              myError: true

to alert('something') it works. I want to use this myError variable to manage submit button - disable it when an error occurs and enable it when it's not.

const [value, setValue] = useState({
    myError: false,
      })

<DateTimePicker
            
            onError = {() => {setValue({
              ...value,
              myError: true
            }) }}
            />

Any ideas what should I change?

1 Answers

onError is a callback with the signature:

Signature:

function(reason: TError, value: TInputValue) => void

reason: The reason why the current value is not valid.

value: The invalid value.

so what you should do there is to set myError if there is a reason, like this:

onError={(reason) => setValue({ myError: Boolean(reason) })}

if you want to go further and show the error reason, you can see this guide on how to.

you can see an example below

Edit aged-haze-q33ffw

Related