Open mgansler opened 4 years ago
What I think is happening is that setTouched
and setValue
are updating state asynchronously and the setTouched
action doesn't have the state with the error and is thus clearing it. Both hooks are using useCallback
. One solution may be to use useEffect
to update state synchronously.
You could move setTouched
to onFocus
.
It also looks like the <Formik/>
component implements these functions imperatively.
🐛 Bug report
Current Behavior
Error message only shows when manually clicking somewhere else (blur)
Expected Behavior
Error message appears as soon as the third option is selected
Reproducible example
Suggested solution(s)
Maybe related:
setValue
andsetTouched
seem to be a new reference each render iteration. Wrapping them inuseCallback
may help.Additional context
Debugging showed that with
oneOf()
, the error message appears for a short time but goes away immediately because the validation is called again with the initial value (which is valid). But that doesn't seem to happen when using some other form of validation. e.g.string().max()
.Your environment