Creating the state

In this section, we are going to implement the type for the state object in our store, along with the initial value for the state. Follow these steps to do so:

  1. Create a new file called Store.ts in the src folder with the following import statement:
import { QuestionData } from './QuestionsData';
  1. Let's create the TypeScript types for the state of our store:
interface QuestionsState {
readonly loading: boolean;
readonly unanswered: QuestionData[] | null;
readonly postedResult?: QuestionData;
}

export interface AppState {
readonly questions: QuestionsState;
}

So, our store is going to have a questions property that, in turn, contains an array of unanswered questions or null in an unanswered property. The questions property includes whether the unanswered questions are being loaded from the server in a loading property. The questions property also includes the result of posting a new question in a postedResult property.

  1. Let's define the initial state for the store so that it has an empty array of unanswered questions and doesn't load questions from the server:
const initialQuestionState: QuestionsState = {
loading: false,
unanswered: null
};

So, defining the state is pretty straightforward. Let's move on and define our actions. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.109.194