Skip to content

Commit

Permalink
fix: SearchBar 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
seung365 committed Nov 20, 2024
1 parent 288f07c commit 0537709
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/components/layouts/SearchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const SearchBar = ({ includeBack = true, includeFavorite = false, goBack }: Sear
const initialSearchWord = searchParams.get('query') || '';
const { setIsModalOpen } = useSearchModalStore();

const { register, handleSubmit, setValue, watch } = useForm<FormValues>({
mode: 'onChange',
const { register, handleSubmit, watch, setValue } = useForm<{ searchWord: string }>({
defaultValues: {
searchWord: initialSearchWord,
},
mode: 'onSubmit',
});

useEffect(() => {
Expand All @@ -42,6 +42,11 @@ const SearchBar = ({ includeBack = true, includeFavorite = false, goBack }: Sear
}
}, [location.pathname]);

useEffect(() => {
const query = searchParams.get('query') || '';
setValue('searchWord', query);
}, [searchParams]);

const searchWord = watch('searchWord');

const generateRandomKey = () => Math.random().toString(36).substr(2, 9);
Expand Down Expand Up @@ -79,13 +84,11 @@ const SearchBar = ({ includeBack = true, includeFavorite = false, goBack }: Sear
}

localStorage.setItem(SEARCH_ARRAY_KEY, JSON.stringify(searchArray));
setSearchParams({ query: searchWord });
setSearchParams({ query: currentSearchWord });
navigate(`/${RouterPath.results}?query=${currentSearchWord}`);
}
};

const nowSearchWord = watch('searchWord');

return (
<SearchBarWrapper>
{includeBack && <IconButton icon="arrow-back" onClick={handleClickBack} />}
Expand All @@ -97,7 +100,7 @@ const SearchBar = ({ includeBack = true, includeFavorite = false, goBack }: Sear
{...register('searchWord')}
onClick={() => setIsModalOpen(true)}
/>
{nowSearchWord?.trim().length > 0 && <CancelIconButton onClick={handleRemoveSearchWord} />}
{searchWord?.trim().length > 0 && <CancelIconButton onClick={handleRemoveSearchWord} />}
</InputBox>
{includeFavorite && <IconButton icon="favorite-default" />}
</SearchBarWrapper>
Expand Down

0 comments on commit 0537709

Please sign in to comment.