From a2f0fb05221eb66175fd91523c2183113c74da1a Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Wed, 30 Mar 2022 14:30:37 +1100 Subject: [PATCH] Chap05 -> Using route parameters -> Implementing more of the question page -> 5. call getQuestion function during initial render --- QandA/frontend/src/QuestionPage.tsx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/QandA/frontend/src/QuestionPage.tsx b/QandA/frontend/src/QuestionPage.tsx index 40465b5..59c4898 100644 --- a/QandA/frontend/src/QuestionPage.tsx +++ b/QandA/frontend/src/QuestionPage.tsx @@ -11,5 +11,14 @@ import { QuestionData, getQuestion } from './QuestionsData'; export const QuestionPage = () => { const [question, setQuestion] = React.useState(null); const { questionId } = useParams(); + React.useEffect(() => { + const doGetQuestion = async (questionId: number) => { + const foundQuestion = await getQuestion(questionId); + setQuestion(foundQuestion); + }; + if (questionId) { + doGetQuestion(Number(questionId)); + } + }, [questionId]); return Question Page {questionId}; };