Improve input value names
This commit is contained in:
parent
09396427b2
commit
035eb2e548
@ -22,7 +22,7 @@ const QUERY_ARTIST_ALBUMS = gql`
|
||||
`;
|
||||
|
||||
export const SearchBox = () => {
|
||||
const [value, setValue] = useState('');
|
||||
const [inputValue, setInputValue] = useState('');
|
||||
const [getArtists, { data }] = useLazyQuery(QUERY_ARTIST_ALBUMS);
|
||||
const [artists, setArtists] = useState<Artists | undefined>(undefined);
|
||||
const [suggestions, setSuggestions] = useState<string[] | undefined>(
|
||||
@ -32,23 +32,22 @@ export const SearchBox = () => {
|
||||
// Debounce the database query, based on the following article:
|
||||
// https://dev.to/reflexgravity/use-lodash-debouce-inside-a-functional-component-in-react-4g5j
|
||||
const updateQuery = () => {
|
||||
getArtists({ variables: { byName: value } });
|
||||
getArtists({ variables: { byName: inputValue } });
|
||||
};
|
||||
|
||||
const delayedQuery = useCallback(debounce(updateQuery, 500), [value]);
|
||||
const delayedQuery = useCallback(debounce(updateQuery, 500), [inputValue]);
|
||||
|
||||
const handleChange = (e: any) => {
|
||||
setValue(e.target.value);
|
||||
setInputValue(e.target.value);
|
||||
};
|
||||
|
||||
// TODO: Maybe merge the two use effects?
|
||||
useEffect(() => {
|
||||
delayedQuery();
|
||||
|
||||
// Cancel previous debounce calls during useEffect cleanup.
|
||||
return delayedQuery.cancel;
|
||||
}, [value, delayedQuery]);
|
||||
}, [inputValue, delayedQuery]);
|
||||
|
||||
// TODO: Maybe merge the two use effects?
|
||||
useEffect(() => {
|
||||
if (data && data.queryArtists !== []) {
|
||||
// Limit artists to 5
|
||||
@ -71,7 +70,7 @@ export const SearchBox = () => {
|
||||
margin={{ vertical: 'large' }}
|
||||
>
|
||||
<TextInput
|
||||
value={value}
|
||||
value={inputValue}
|
||||
onChange={handleChange}
|
||||
placeholder='Type an artist name'
|
||||
icon={<FormSearch color='plain' />}
|
||||
|
Loading…
Reference in New Issue
Block a user