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

Chap05
Jason Zhu 2022-03-30 14:30:37 +11:00
parent a51437c7cc
commit f50dd73596
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>;
};