Chap05 -> Using query parameters -> 6. Invoke search when component first renders and when the search variable changes using the useEffect hook

master
Jason Zhu 2022-03-30 16:39:03 +11:00
parent 1f05de9a0c
commit 269e6173d1
1 changed files with 9 additions and 0 deletions

View File

@ -10,6 +10,15 @@ import { searchQuestions, QuestionData } from './QuestionsData';
export const SearchPage = () => {
const [searchParams] = useSearchParams();
const [questions, setQuestions] = React.useState<QuestionData[]>([]);
const search = searchParams.get('criteria') || '';
React.useEffect(() => {
const doSearch = async (criteria: string) => {
const foundResults = await searchQuestions(criteria);
setQuestions(foundResults);
};
doSearch(search);
}, [search]);
return <Page title="Search Results">{null}</Page>;
};