Welcome to a new React JS project, where we are going to build a Flashcard Quiz app using React.
So, fire up your terminal and create a new react app flashcard-react with the below command.
We will remove all the unnecessary boiler-plate code and our index.js will look like below.
And the App.js contains only Hello World text.
Next, create a components folder src folder and two files FlashcardList.js and Flashcard.js inside it. Next, we will use sample question data before, getting the data from the API endpoint for the quiz.
So, in App.js create a variable SAMPLE_CARDS, which contains various things related to the question as objects inside an array.
Next, we will create an state variable cards and assign it to SAMPLE_CARDS using useStatehook. After that we will pass the cards to the FlashcardList component as flashcards props.
Next, head over to FlashcardList.js file and use the flashcards props. We are mapping over it and passing individual object to the Flashcard component.
Now, in the Flashcard.js file, we are just showing the question for now.
Now, in localhost we can see the three questions.
We will next implement the flip logic in cards. So, in Flashcard.js we will use the state variable flip and set it to false. The click of the div will set it to toggle and display the question or answer accordingly.
So, if we move to localhost we can see the flipping in action.
We will now complete our Flashcard.js component with the html structure.
Now, we will create our styles. So, create a new file App.css and also include it in App.js file.
Now, in App.css we will put the basic styles, plus the styles for card.
If we go to localhost we can see the cards and also the answer in back with 180deg rotation.
Now, we don’t want the backside of the card to be shown. So, we will give the backface-visibility: hidden property.
Next, we want to flip the card based on the click. For that we are having a transform on the card, where are creating a variable rotate-y, which is set to 180deg on flip. We also need to have transform-style: preserve-3d for this to work.
So, now in localhost we can see this in action.
Flipper in action
We will add two more properties for nice smoother transition. We will also have a nice box-shadow on hover of the box.
We will also have style for the options in card. So, put the in App.css.
Now, it’s time to hit the API endpoint and generate the questions for our quiz. We will be using the open trivia DB for the same.
For using the API, we need axios. So, install it and use the useEffect hook to cal the opentdb endpoint once.
Now, we will create our array of object similar to SAMPLE_CARDS in the .then block. Here, we are also using decodeString function in question, because we are receiving HTML characters also in some questions.
Now, we will put styles in our App.css file. Here, we are styling the card-grid and also making the height more and removing the width.
I found two small bugs in above. The first to show a question from SAMPLE_CARDS and that can be fixed with passing an empty array to useState.
The second thing is that we need decodeString() in everything, because sometime the answers also contained special HTML characters.
Now, our small flashcard quiz app is complete with 10 random questions from Trivia DB. You can find code for the same in this github repo.