Lesson 152 of 158 – Project Student Details
96%

Project Student Details

In this lesson, we will create the Student Details screen for our Student Management mobile application using React Native, TypeScript, Axios, and the PHP REST API.

The screen will receive a student ID, request the student's details from the protected API, and display the complete student profile.

Project Goal: Create a protected Student Details screen that retrieves one student from the PHP/MySQL REST API using Axios and JWT authentication.

1. Student Details Screen Flow

Student List
      ↓
Select Student
      ↓
Pass Student ID
      ↓
Student Details Screen
      ↓
Axios GET Request
      ↓
JWT Authentication
      ↓
PHP REST API
      ↓
MySQL Database
      ↓
Student JSON
      ↓
Display Profile

2. Why Create a Details Screen?

The Student List should show basic information, while the Details screen can display complete information about one student.

  • Student name
  • Email
  • Mobile number
  • Course
  • Address
  • Student ID
  • Other profile information returned by the API

3. Student TypeScript Interface

interface Student {
    id: number;
    name: string;
    email: string;
    mobile: string;
    course: string;
    address: string;
}

This interface describes the student object returned by the API.

4. Student Profile Response

interface StudentProfileResponse {
    success: boolean;
    message?: string;
    data: Student;
}

The API can return the student inside the data property.

5. Import React Native Components

import React, {
    useEffect,
    useState
} from "react";

import {
    View,
    Text,
    ActivityIndicator,
    ScrollView,
    TouchableOpacity
} from "react-native";

6. Student State

const [student, setStudent] =
    useState<Student | null>(null);

const [loading, setLoading] =
    useState(true);

const [error, setError] =
    useState("");

The student starts as null because the API data has not been loaded yet.

7. Receiving Student ID

React Navigation can pass the student ID from the Student List screen.

const { id } = route.params;

For example, the Details screen may receive:

{
    id: 5
}

8. Student Profile API

Our project can use a protected endpoint such as:

GET /api/student_profile.php?id=5

The ID identifies which student should be returned.

9. Axios GET Request

const response =
    await api.get<StudentProfileResponse>(
        "/student_profile.php",
        {
            params: {
                id
            }
        }
    );

setStudent(
    response.data.data
);

10. Load Student Details

const loadStudent =
    async () => {

    try {

        setLoading(true);
        setError("");

        const response =
            await api.get<StudentProfileResponse>(
                "/student_profile.php",
                {
                    params: {
                        id
                    }
                }
            );

        setStudent(
            response.data.data
        );

    } catch (error) {

        setError(
            "Unable to load student details."
        );

    } finally {

        setLoading(false);

    }
};

11. useEffect for API Request

useEffect(() => {

    loadStudent();

}, [id]);

The request runs when the screen opens and whenever the student ID changes.

12. Loading State

if (loading) {

    return (
        <View>

            <ActivityIndicator
                size="large"
            />

            <Text>
                Loading student...
            </Text>

        </View>
    );

}

A loading indicator provides feedback while the API request is running.

13. Error State

if (error) {

    return (
        <View>

            <Text>
                {error}
            </Text>

        </View>
    );

}

14. Display Student Name

<Text>
    {student?.name}
</Text>

Optional chaining prevents an error if the student object is temporarily null.

15. Display Student Information

<Text>
    Name: {student?.name}
</Text>

<Text>
    Email: {student?.email}
</Text>

<Text>
    Mobile: {student?.mobile}
</Text>

<Text>
    Course: {student?.course}
</Text>

<Text>
    Address: {student?.address}
</Text>

16. Using ScrollView

<ScrollView>

    <Text>
        Student Profile
    </Text>

    <Text>
        {student?.name}
    </Text>

    <Text>
        {student?.email}
    </Text>

    <Text>
        {student?.mobile}
    </Text>

</ScrollView>

ScrollView is useful when the profile contains several fields.

17. Student Profile Card

<View>

    <Text>
        Student Profile
    </Text>

    <Text>
        Name: {student?.name}
    </Text>

    <Text>
        Email: {student?.email}
    </Text>

    <Text>
        Mobile: {student?.mobile}
    </Text>

    <Text>
        Course: {student?.course}
    </Text>

    <Text>
        Address: {student?.address}
    </Text>

</View>

18. View Student from Student List

From the Student List, navigate to the Details screen and pass the selected student's ID.

navigation.navigate(
    "StudentDetails",
    {
        id: student.id
    }
);

The Details screen then uses this ID to request the correct record.

19. React Navigation Type

type RootStackParamList = {
    StudentList: undefined;
    StudentDetails: {
        id: number;
    };
};

TypeScript can ensure that the Details screen receives a numeric student ID.

20. Handle 401 Authentication Error

catch (error) {

    if (
        axios.isAxiosError(error) &&
        error.response?.status === 401
    ) {

        setError(
            "Session expired. Please login again."
        );

    } else {

        setError(
            "Unable to load student."
        );
    }
}

A 401 response usually means that authentication is missing or the token is no longer valid.

21. Handle 404 Student Not Found

if (
    axios.isAxiosError(error) &&
    error.response?.status === 404
) {

    setError(
        "Student not found."
    );

}

The API should return 404 when the requested student does not exist.

22. API Security

  • Require JWT authentication.
  • Verify the token on the server.
  • Validate the student ID.
  • Use prepared SQL statements.
  • Return only safe student fields.
  • Never return passwords or password hashes.
  • Use HTTPS in production.

23. PHP Student Profile Request

$id = filter_input(
    INPUT_GET,
    'id',
    FILTER_VALIDATE_INT
);

if (!$id) {

    http_response_code(400);

    echo json_encode([
        "success" => false,
        "message" => "Invalid student ID"
    ]);

    exit;
}

The API should validate the ID before querying the database.

24. PHP Database Query

$stmt = $pdo->prepare(
    "SELECT
        id,
        name,
        email,
        mobile,
        course,
        address
     FROM students
     WHERE id = ?"
);

$stmt->execute([$id]);

$student = $stmt->fetch(
    PDO::FETCH_ASSOC
);

A prepared statement protects the database query from SQL injection.

25. PHP JSON Response

if (!$student) {

    http_response_code(404);

    echo json_encode([
        "success" => false,
        "message" => "Student not found"
    ]);

    exit;
}

http_response_code(200);

echo json_encode([
    "success" => true,
    "data" => $student
]);

26. Complete React Native Details Screen

const StudentDetailsScreen =
    ({ route }: any) => {

    const { id } = route.params;

    const [student, setStudent] =
        useState<Student | null>(null);

    const [loading, setLoading] =
        useState(true);

    const [error, setError] =
        useState("");

    const loadStudent =
        async () => {

        try {

            setLoading(true);
            setError("");

            const response =
                await api.get<StudentProfileResponse>(
                    "/student_profile.php",
                    {
                        params: {
                            id
                        }
                    }
                );

            setStudent(
                response.data.data
            );

        } catch (error) {

            if (
                axios.isAxiosError(error) &&
                error.response?.status === 404
            ) {

                setError(
                    "Student not found."
                );

            } else {

                setError(
                    "Unable to load student."
                );
            }

        } finally {

            setLoading(false);

        }
    };

    useEffect(() => {

        loadStudent();

    }, [id]);

    if (loading) {

        return (
            <View>
                <ActivityIndicator
                    size="large"
                />
                <Text>
                    Loading student...
                </Text>
            </View>
        );
    }

    if (error) {

        return (
            <View>
                <Text>
                    {error}
                </Text>
            </View>
        );
    }

    return (

        <ScrollView>

            <Text>
                Student Details
            </Text>

            <Text>
                ID: {student?.id}
            </Text>

            <Text>
                Name: {student?.name}
            </Text>

            <Text>
                Email: {student?.email}
            </Text>

            <Text>
                Mobile: {student?.mobile}
            </Text>

            <Text>
                Course: {student?.course}
            </Text>

            <Text>
                Address: {student?.address}
            </Text>

        </ScrollView>
    );
};

27. Add Edit and Delete Actions

The Details screen can provide navigation to the Edit Student screen and can also connect to the Delete API.

navigation.navigate(
    "EditStudent",
    {
        id: student.id
    }
);

For deletion, the application should first ask the user for confirmation and then call the DELETE API.

28. Student Details Screen Architecture

Student List
      ↓
Student ID
      ↓
React Navigation
      ↓
Student Details
      ↓
Axios
      ↓
JWT Interceptor
      ↓
PHP API
      ↓
JWT Verification
      ↓
PDO + MySQL
      ↓
Student Record
      ↓
JSON Response
      ↓
TypeScript
      ↓
Student Profile UI

29. Complete Project Flow

Login
  ↓
JWT Token
  ↓
Student List
  ↓
Select Student
  ↓
Pass Student ID
  ↓
Student Details
  ↓
GET /student_profile.php?id=5
  ↓
Verify JWT
  ↓
Validate ID
  ↓
Query MySQL
  ↓
Return JSON
  ↓
Display Student
  ↓
Edit / Delete

30. Project Student Details Summary

The Student Details screen completes the profile-viewing part of our Student Management application.

  • The Student ID is passed from the Student List screen.
  • Axios sends the GET request to the PHP REST API.
  • JWT protects the API request.
  • The PHP API validates the student ID.
  • PDO retrieves the student from MySQL.
  • The API returns structured JSON.
  • TypeScript interfaces describe the response.
  • Loading and error states improve the user experience.
  • 404 handles a missing student.
  • 401 handles authentication problems.
  • The next lesson will create the Add Student screen.

📌 Key Points

  • Use a student ID to retrieve one student.
  • Use Axios for communication with the REST API.
  • Use TypeScript interfaces for API responses.
  • Use JWT authentication for protected endpoints.
  • Validate the student ID on the server.
  • Use PDO prepared statements for database queries.
  • Handle 401 and 404 responses properly.
  • Never return passwords or password hashes.
  • React Navigation can pass the selected student ID.
  • The Details screen can connect to Edit and Delete functionality.

🧠 Quick Quiz

Question: Which HTTP method should normally be used to retrieve a student's details from the REST API?