I want to immediately see the cookbook state variable filled with the data from the API get request. It is not updating in time for the likedByUser
function. If I add cookbook as a dependency in the useEffect()
dependency array, then it goes into an infinite rendering loop. Any ways for me to see the updated cookbook array by the time I get to the likedByUser
function?
const RecipeList = (props) => {
const [recipes, setRecipes] = useState([]);
const [cookbook, setCookbook] = useState([]);
const cookbookURL = 'https://url';
const getCookbook = async () => {
const axiosAuth = await axiosWithAuth();
const res = await axiosAuth.get(cookbookURL); //resolves successfully
setCookbook(res.data);
}
const likedByUser = () => {
console.log('cookbook',cookbook); //cookbook comes back as an empty array
}
useEffect(() =>{
getCookbook();
likedByUser(); //I want to access the cookbook state variable here
setRecipes(props.recipes);
},[]);
return <View>{cookbook.map(recp => <Recipe recipe={recp} />)}</View>