Here's a rough idea of how you could do it, based on the description of your data structures. You didn't share much about the structures, so I used string IDs (but maybe you have them in arrays and you could use integer indexes instead).
The important part is the demonstration of how one effect hook can update a state value that the other one depends on, and the next one can react and update more state.
Then, you conditionally render different JSX depending on your state.
If you're unfamiliar with TypeScript and TS syntax, you can follow the playground link below, and you'll see the plain compiled JavaScript on the right
TS Playground
import {default as React, useEffect, useState} from 'react';
// Some arbitrary types for this example
type Student = {
id: string;
};
type Classroom = {
id: string;
students: { [id: string]: Student };
};
type Class = {
id: string;
classRooms: { [id: string]: Classroom };
};
// Functions which get the data from your API. Imagine these are in another file and you import them:
// imoprt {fetchClass, fetchClassroom} from './api';
declare function fetchClass (id: string): Promise<Class>;
declare function fetchClassroom (id: string): Promise<Classroom>;
// Determine if a value exists (is not null or undefined)
function exists <T>(maybe: T): maybe is NonNullable<T> {
return maybe != null;
}
// The component
function Example () {
const [classId, setClassId] = useState<string>();
const [currentClass, setCurrentClass] = useState<Class>();
const [classroomId, setClassroomId] = useState<string>();
const [currentClassroom, setCurrentClassroom] = useState<Classroom>();
useEffect(() => {
if (!exists(classId)) return;
(async () => {
setCurrentClass(await fetchClass(classId));
setClassroomId('id_of_first_classroom');
})();
}, [classId]);
useEffect(() => {
if (!exists(classroomId)) return;
(async () => {
setCurrentClassroom(await fetchClassroom(classroomId));
})();
}, [classroomId]);
if (exists(currentClass)) {
if (exists(currentClassroom)) {
// return JSX created with the currentClass and currentClassroom data
}
else {
// return JSX created with just the currentClass data (e.g. choose a classroom based on a list of classroom IDs)
}
}
// return some JSX to get started (e.g. choose a class based on a list of class IDs)
}