Question:
How to prevent submit if value hasn't changed?
I tried to do this using useState.
Snippet:
import React from 'react'
import {Formik, Form, Field} from 'formik'
const Search = () => {
const [searchValue, setSearchValue] = useState('')
const handleSubmit = q => {
window.location.href = `https://for-example.com/search?q=${q}`
}
return (
<Formik onSubmit={({q}, {setSubmitting}) => {
setSubmitting(false)
setSearchValue(q) // <===
return q !== searchValue && handleSubmit(q) // <===
}} initialValues={{q: ''}} render={() => (
<Form>
<Field name='q' />
</Form>
)}/>
)
}
P.S. This works but I think it's not worth creating useState for this simple task. Can I check the search value in another way?