Chap05 -> Using route parameters -> Implementing more of the question page -> 5. call getQuestion function during initial render

master
Jason Zhu 2022-03-30 14:30:37 +11:00
parent de5e78e621
commit a2f0fb0522
1 changed files with 9 additions and 0 deletions

View File

@ -11,5 +11,14 @@ import { QuestionData, getQuestion } from './QuestionsData';
export const QuestionPage = () => {
const [question, setQuestion] = React.useState<QuestionData | null>(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 <Page>Question Page {questionId}</Page>;
};