Let’s Build a Fun Quiz App with React Native!

Harry Bloch
4 min readMay 25, 2023

--

In the rapidly evolving landscape of mobile applications, proficiency in a versatile framework like React Native is an invaluable skill. Today, we’ll focus on honing your React Native skills by building a simple, yet robust Quiz application. This guide is tailored for beginners, offering a step-by-step walkthrough to ensure a comprehensive understanding of the process. By the end of this tutorial, you’ll have a functional Quiz application and a solid foundation in React Native development.

Oh, and before we jump in, don’t forget to check out some of my awesome apps on the App Store! Shameless self-promotion, right? Let’s get started!

Step 1: Setup Your Coding Playground

Before we start playing with code, we need to set up our coding playground. First, we need Node.js installed on our machine. Head to the Node.js website and download the latest LTS version.

Next, we'll use Expo CLI, which will help us lay the foundation for our new project. Install it by running this command in your terminal:

npm install -g expo-cli

Step 2: Unleash the React Native Project

With the Expo CLI installed, it's time to create our project. Navigate to your desired folder and type the following in your terminal:

expo init QuizWhiz

Select the blank template when prompted.

Step 3: Wake Up, App!

Navigate into your newly created project folder using cd QuizWhiz, and type expo start to run your app. You'll see a QR code appear in your terminal. Download the Expo app from your phone's app store, scan the QR code, and presto! Your app is alive and running!

Step 3: Crafting the Quiz Screen

Now, let's dive into some fun code. Create a new file in your project directory named QuizScreen.js and write the following code:

import React from 'react';
import { View, Text } from 'react-native';

const QuizScreen = () => {
return (
<View>
<Text>Welcome, Quiz Whiz!</Text>
</View>
);
}

export default QuizScreen;

This snippet creates a new screen with a catchy welcome message.

Step 4: Navigating with React Navigation

To move between different screens, we need to install React Navigation. Run these commands in your terminal:

npm install @react-navigation/native
expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
npm install @react-navigation/stack

Now, replace the existing content of App.js with the following:

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import QuizScreen from './QuizScreen';

const Stack = createStackNavigator();

const App = () => {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Quiz">
<Stack.Screen name="Quiz" component={QuizScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}

export default App;

Run your app again with expo start, and you should see the "Welcome, Quiz Whiz!" message.

Step 5: The Quiz Logic

We're now ready for the heart of our app, the quiz logic! We're going to have an array of questions with multiple choices, and track the user's score.

Modify the QuizScreen component:

import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';

const QuizScreen = () => {
const questions = [
{ questionText: 'What is the capital of France?', answerOptions: ['Paris', 'Berlin', 'London', 'Rome'], correctAnswer: 'Paris' },
{ questionText: 'Who wrote "To Kill a Mockingbird"?', answerOptions: ['George Orwell', 'J.K. Rowling', 'Harper Lee', 'Stephen King'], correctAnswer: 'Harper Lee' },
// Add more questions as needed!
];

const [currentQuestion, setCurrentQuestion] = useState(0);
const [score, setScore] = useState(0);

const handleAnswerOptionClick = (answer) => {
const correctAnswer = questions[currentQuestion].correctAnswer;
if (answer === correctAnswer) {
setScore(score + 1);
}
const nextQuestion = currentQuestion + 1;
if (nextQuestion < questions.length) {
setCurrentQuestion(nextQuestion);
} else {
alert(`Quiz finished! You scored ${score} out of ${questions.length}`);
}
};

return (
<View>
<Text>{questions[currentQuestion].questionText}</Text>
{questions[currentQuestion].answerOptions.map((answerOption) => (
<Button title={answerOption} onPress={() => handleAnswerOptionClick(answerOption)} />
))}
</View>
);
};

export default QuizScreen;

You’ve successfully built a fully functional Quiz application with React Native. This achievement is a testament to your dedication and ability to learn new technologies. Remember, this is only the tip of the iceberg when it comes to what’s possible with React Native. You’re now equipped with the knowledge to explore more advanced features, such as timers, different question categories, or varying levels of difficulty.

Never stop learning, never stop growing. Keep enhancing your skills and applying them to more complex projects. For inspiration, be sure to explore our range of innovative apps at App Store. Each app offers a unique insight into the capabilities of React Native, and they could serve as excellent templates for your next project.

Your journey into the world of app development is just getting started. Stay curious, stay focused, and continue turning your ideas into reality. Happy coding!

--

--