...

/

Implementing Form Submission in the Ask Form

Implementing Form Submission in the Ask Form

Learn to implement the submission form in the ask form.

We'll cover the following...

Let’s carry out the following steps to implement submission in the ask form:

  1. In QuestionsData.ts, create a function that will simulate posting a question:

Press + to interact
export interface PostQuestionData {
title: string;
content: string;
userName: string;
created: Date;
}
export const postQuestion = async (
question: PostQuestionData,
): Promise<QuestionData | undefined> => {
await wait(500);
const questionId =
Math.max(...questions.map(q => q.questionId)) + 1;
const newQuestion: QuestionData = {
...question,
questionId,
answers: [],
};
questions.push(newQuestion);
return newQuestion;
};

This function adds the question to the questions array using the Math.max method to set questionId to the next number.

  1. In AskPage.tsx, import the function we just added to QuestionData.ts:

Press + to interact
import { postQuestion } from './QuestionsData';
...