How to use a state variable inside of a useEffect hook

  Kiến thức lập trình
useEffect(() => {
    GetInterviewDetails();
},[]);


const GetInterviewDetails = async () => {
  const result = await db
    .select()
    .from(MockInterview)
    .where(eq(MockInterview.mockId, params.interviewId));
  setInterviewData(result[0]);
}; 

I’m writing a web app and I’m trying to use my setter function setInterviewData to populate interviewData state. I have checked that result var is getting the data from my db. I understand that setting state is async and thats why the interviewData state is undefined upon render. Are there any suggestions about how I could get this to work and properly insert my db data into the interviewData state?

Ive tried using dummy state var to try and see if I can get it to change but because of the async this also has not worked. If I set the dependency array to interviewData I get infinite rerenders because of it resetting the state so I know that doesnt work.

New contributor

Hi im Purge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

7

LEAVE A COMMENT