I have been trying to retrieve data from an API using the getinitialprops method and axios
However, it seems like my code is not working as expected. Here is a snippet of the relevant code in the file pages/index.tsx
IndexPage.getInitialProps = async (ctx: any) => {
try {
const res = await axios.get(
"THE_PATH"
);
const restaurants = res.data;
return { restaurants };
} catch (error) {
return { error };
}
};
After implementing the getinitialprops function, I tried to use it in the following way:
const IndexPage = (props: any) => {
<div
onClick={() => {console.log(props)}}>
Unfortunately, it doesn't display the props as expected. What could be missing in my implementation?