I am trying to implement search with formik #3131
Unanswered
anurag55555
asked this question in
Q&A
Replies: 1 comment
-
I would do this as an effect, so I'd create a child element like: // Trigger a search anytime the value of searchText changes and doesn't change again for 250ms
const AutoSearch = (props) => {
const { values } = useFormikContext();
const { searchFunction } = props;
// debounce here so the search only happens when a user stops typing for 250ms
// this uses lodash but you can use any debounce, or a useDebounce hook.
// searchFunction must be memoized and should never change!
const debouncedSearchFunction = useMemo(() => _.debounce(searchFunction, 250), [searchFunction]);
useEffect(() => {
values.searchText && debouncedSearchFunction(values.searchText);
}, [values, debouncedSearchFunction]);
return null; // or print some kind of result like autosuggestions
}
const MyForm = (props) => {
return <Formik {...formikProps}>
{(formik) => (
<TextField
handleChange={formik.handleChange}
handleBlur={formik.handleBlur}
value={formik.values.searchText}
name="searchText"
// etc
/>
<AutoSearch searchFunction={props.searchFunction} />
)}
</Formik>
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What i am trying to achieve is a search box where you type and search operation happens simultaneously.
what i have done:
searchFunction is sent via Props from parent file where API call is taking place using value in searchText as user type
I am afraid if this is the right way
Beta Was this translation helpful? Give feedback.
All reactions