When i try to add in a new ticket, it isn't rendering on the screen properly, only after…
- User
When i try to add in a new ticket, it isn't rendering on the screen properly, only after i come back to that page the ticket is added on the table fix it...import React, { useEffect, useState } from "react"; import { useParams } from "react-router-dom"; import axios from "axios"; import { getJwtToken } from "../GlobalVar/JwtToken"; import { getUserObject } from "../GlobalVar/UserObject"; import "./ProjectView.css"; import AddTicket from "../component/Modal/AddTicket";
function ProjectView() { const [project, setProject] = useState(null); const [userObject, setUserObject] = useState(null); const [showAddTicketModal, setShowAddTicketModal] = useState(false); const params = useParams();
const handleAddMember = () => {};
const handleAddTicket = async () => { setShowAddTicketModal(true); };
const handleEditTicket = (ticketId) => {};
const handleRemoveTicket = (ticketId) => {};
useEffect(() => { const jwtToken = getJwtToken();
codeconst fetchProject = async () => { try { const response = await axios.get( `http://localhost:8080/dashboard/project/${params.projectId}`, { headers: { Authorization: `Bearer ${jwtToken}`, }, } ); setProject(response.data); } catch (error) { console.error("Error fetching project:", error); } }; const fetchUserObject = async () => { try { const userObject = await getUserObject(); setUserObject(userObject); } catch (error) { console.error("Error fetching user object:", error); } }; fetchProject(); fetchUserObject();}, []);
const handleTicketAdded = (ticket) => { setProject((prevProject) => { const updatedTickets = [...prevProject.tickets, ticket]; const updatedProject = { ...prevProject, tickets: updatedTickets }; return updatedProject; }); setShowAddTicketModal(false); };
if (!project || !userObject) { return Loading...; }
return ( Project Details Project ID Project Name Description Project Identifier Date Created Last Updated {project.projectId} {project.projectName} {project.description} {project.projectIdentifier} {project.dateCreated} {project.lastUpdated}
code - Assistant
In the
ProjectViewcomponent, I see that you are fetching the project data in theuseEffecthook, but you are not adding a dependency array to listen for changes in the component. This might be causing issues with ticket rendering on the screen. You should add a dependency array to theuseEffecthook for project changes.Here's what you need to do:
-
Add a dependency array
[project]to theuseEffecthook in theProjectViewcomponent. -
Modify the way you're updating the tickets in the state after adding a new ticket. Instead of directly updating the state, use a function inside
setStateto update the tickets array. -
In
AddTicket, when submit button is clicked, disable the button by setting
-
- User
it infinitly keeps rendering...
- Assistant
Apologies for the confusion. I misunderstood your requirements earlier. You are correct that adding
projectto the dependency array will cause an infinite render. Instead, we should store theparams.projectIdinside a variable and use it as a dependency.Here's the corrected approach:
- Remove the
[project]dependency from theuseEffecthook. - Add a new variable called
projectIdinside theProjectViewcomponent, and use it to storeparams.projectId. - Add
[projectId]as a dependency of theuseEffecthook.
Update your code in
ProjectView.jsas follows:jsx - Remove the
- User
it still renders it imporprly, i have to come back to the page for it to show up on the screen
- Assistant
I apologize for the confusion in my previous response. After closely analyzing the code, I found the issue:
The problem is that the updated ticket data is being fetched asynchronously, but the table is rendering the old state before the new data arrives.
Change your React state update code to force the component to re-fetch the project after the ticket has been added, using an additional state,
refreshProject.Here's how to fix it in
ProjectView.js:- Add a new state variable called
refreshProjectwith a default value offalse.
- Add a new state variable called