I have a react component that gets data from an API end point so that it can render some HTML.
I know that the call to the API works becuase I can out put the response in console.log(). However when I call the set function defined in the Use State so that I can set the state object when I console.log() that value it is empty.
This is my first react project so I may be missing something every fundamental, can only exaplain whats going on here?
This is the code so far:
function RenderCmsComponents() {
const location = useLocation();
const [componentItems, setCmsComponents] = useState([]);
const getComponents = async () => {
const componentList = await API.fetchPageComponents(location.pathname);
setCmsComponents(componentList.CmsComponents);
console.log(componentList.CmsComponents); //Works
console.log(componentItems); // this is Empty, why?
};
useEffect(() => {
getComponents();
}, []);
return (
<div>
<p> Render CMS Components for `{location.pathname}`</p>
</div>
);
}
export default RenderCmsComponents;