Set the data in React Context from asynchronous API call Set the data in React Context from asynchronous API call reactjs reactjs

Set the data in React Context from asynchronous API call


try to use useContext its cleaner and try not to use the async inside the useEffect!

their related issues

import React, { useState,useEffect,useContext } from "react";import { callAffiliateApi } from "./services/affiliateService/transactionContext";const Context = React.createContext({});const AffiliateContext = init => useContext(Context);export const AffiliateProvider  = ({children}) => {  const [data, setData] = useState(null);  const [loading,setLoading]=useState(false);  const getAffiliates = async ()=>{      setLoading(true)      const newText = await callAffiliateApi();      setData(newText)      setLoading(false)  }  useEffect(()=> {    getAffiliates()  },[])    return (      <AffiliateContext.Provider value={{loading,list:data}}>        {children}      </AffiliateContext.Provider>    )}