React useContext – wait for api to finish

const tableContext = React.createContext();
const tableContextProvider = ({children}) => {
   const [isTableOne, setTableOne] = useState(false);
   const [isTableTwo, setTableTwo] = useState(false);

   useEffect(() => {
       setTableValues();
   }, []);

   function setTableValue() {
       // api call returns result
       setTableOne(result.IS_TABLE_ONE); // true
       setTableTwo(result.IS_TABLE_TWO); // false
   }

   return (<TableContext.Provider value={{isTableOne, isTableTwo}}>{children}</TableContext.Provider>);
};

In a different component, the result shows:

{isTableOne:false, isTableTwo:false}
{isTableOne:true, isTableTwo:false}

It returns two objects and it takes some time to return the second one. How could I make it wait and only return the last result?

106 thoughts on “React useContext – wait for api to finish”

  1. 38939 122159Your weblog is one of the much better blogs Ive came across in months. Thank you for your posts and all the very best along with your function and blog. Looking forward to reading new entries! 340208

    Reply
  2. 728340 560274Couldn?t be developed any greater. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this report to him. Pretty certain he will possess a excellent read. Thanks for sharing! 757414

    Reply

Leave a Comment