I am new to React and am having a difficult time figuring out how I can wait for the state to have a specific (not null) update before fetching data. I am using firebase JWT and am passing the token into the headers but with my current code it runs and passed the value of null. Is there a nifty hook trick to ensure that my fetchData function only runs once and that it only runs after the token value is set?
I tried setting the state as  const [token, setToken] = useState(auth.currentUser.getIdToken()); but it appears to return a promise into the header and not the token (guessing its because its async). Thanks!
import React, { useState, useEffect } from 'react';
import { auth } from '../../firebase-config';
const RecordEntry = (props) => {
  
  const [token, setToken] = useState();
  const [isLoading, setIsLoading] = useState(false);
    var mydata = 
     {
        entry_id = props.entry_id
    }
  //should only call this once
  const fetchData = async () => {
      const current_token = auth.currentUser.getIdToken();
      setToken(current_token);
      //need to yield here to verify token is set and not null - this is where I am stuck
      fetch('https://mysite/api/recordEntry' , {
        method: 'POST',
        headers: new Headers({ 
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`,
        }),
        body: JSON.stringify(mydata)
        })
      .then((response) => response.json())
      .then((data) => {
        setIsLoading(false);
      })
      .catch((error) => {
        setIsLoading(false);
        console.log(error);
      });
  };
//passing empty array so the effect only runs once
  useEffect(() => {
    fetchData();
}, []);
  if (isLoading) {
    return <div>Loading...</div>;
  }
  return (
    <div>
        <h1> Entry Recorded </h1>
    </div>
  );
};
export default RecordEntry;
 
     
    