Answer the question
In order to leave comments, you need to log in
React Query - how to clear a form on successful form submission?
I made a form and screwed Formik
<form onSubmit={formik.handleSubmit}>
<input id="chapter" onChange={formik.handleChange} type="text" placeholder="Раздел" required />
***
<button>Сохранить</button>
</form>
const mutation = useMutation((item) =>
axios({
method: "POST",
url: "https://api.***",
data: item
})
);
if (mutation.isSuccess){
}
Answer the question
In order to leave comments, you need to log in
+1 вариант
const input = useRef(null)
...
function successSubmit() {
input.current.value = ""
}
...
<Input ref={input } />
+2 вариант через state
const [state, setState] = useState({
inputValue: ''
})
...
function successSubmit() {
setState(prevState => {
return {
...prevState,
inputValue: ''
}
})
}
...
<Input
value={state.inputValue}
onInput={e => {
setState(prevState => {
return {
...prevState,
inputValue: e.target.value
}
})
}}/>
+3 Вариант изощренный - "до лучших времен"
function successSubmit() {
document.querySelectorAll('input').forEach(i => i.value = '')
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question