Hi all,
I am following along in the NextJS course building the Issue Tracker. I am on the section called "Populate the Assignee dropdown. In the code he is using the useEffect() hook. He does not explain why, just says go back to my React course. So far, this is the only code that uses that hook and would like to know why.
"use client";
import { User } from "@prisma/client";
import { Select } from "@radix-ui/themes";
import axios from "axios";
import React, { useEffect, useState } from "react";
const AssigneeSelect = () => {
const [users, setUsers] = useState<User[]>([]);
useEffect(() => {
const fetchUsers = async () => {
const { data } = await axios.get<User[]>("/api/users");
setUsers(data);
};
fetchUsers();
}, []);
return (
<Select.Root>
<Select.Trigger placeholder="Assign..." />
<Select.Content>
<Select.Group>
<Select.Label>Suggestions</Select.Label>
{users.map((user) => (
<Select.Item key={user.id} value={user.id}>
{user.name}
</Select.Item>
))}
</Select.Group>
</Select.Content>
</Select.Root>
);
};
export default AssigneeSelect;
Initially I thought it was because we were calling back end code to get data when the component is set to “use client”, however, the course has other components that are set to run client side and call back end code and the useEffect() hook is not used:
"use client";
import { AlertDialog, Button, Flex, Spinner } from "@radix-ui/themes";
import axios from "axios";
import { useRouter } from "next/navigation";
import { useState } from "react";
const DeleteIssueButton = ({ issueId }: { issueId: number }) => {
const router = useRouter();
const [hasError, setError] = useState(false);
const [isDeleting, setDeleting] = useState(false);
const deleteIssue = async () => {
try {
setDeleting(true);
await axios.delete("/api/issues/" + issueId);
router.push("/issues/list");
router.refresh();
} catch (error) {
setError(true);
setDeleting(false);
}
};
return (
<> ..... markup here...
Can someone please explain why useEffect is needed in this case and why it would be used in NextJS at all?
Thank you,
Jim