Using useState to implement component state

The time has come to implement state in the HomePage component so that we can store any unanswered questions. But how do we do this in function-based components? Well, the answer is to use another React hook called useState. Follow the steps listed in HomePage.tsx to do this:

  1. Add the useState hook to the React import and the QuestionData interface to the QuestionsData import:
import { useEffect, useState } from 'react';...import {   getUnansweredQuestions,   QuestionData } from './QuestionsData';
  1. We'll use this hook just above the useEffect statement in the HomePage component to declare the state variable:
const [questions, setQuestions]   = useState<QuestionData[] | null>(null);useEffect(() ...

Get ASP.NET Core 3 and React now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.