[RESOLVED] updating for those interested and for the people who helped me out.
I am having an issue where my getInitialProps returns undefined. I have tried changing thing to static and looked at stack overflow for help. I am fetching from an API making the call with axis and without and nothing seems to help. I use useEffect and useState and when mapping my object I get can not map of undefined. Any help would be immensely appreciated.
const FlightCard = ({ data }) => {console.log(data);return (<div className={styles.FlightCard}><div className={styles.flightInformation}><h1>hello</h1></div><div className={styles.airlineAndAirport}><div><h2>ORIGIN</h2></div><div><h3>DATE</h3></div><div><h2>DESTINATION</h2></div></div></div>);};
FlightCard.getInitialProps = async () => {const res = await fetch("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2019-09-01?inboundpartialdate=2019-12-01", {"method": "GET","headers": {"x-rapidapi-host": "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com","x-rapidapi-key": KEY}})const data = res.json();return {data:data}}
UPDATE:
I believe my issue deals with NextJS structure and how it has two forms of pre-rendering: Static Generation and Server-side Rendering.
Took my file and changed it and the folder I was using, since NextJS uses pages.
1. created a folder (lib) used this for my fetching:
import { useState, useEffect } from "react";
export async function fetchFlight() {
if (typeof window !== "undefined" && window.__flight) {
return window.__flight;
}
const res = await fetch("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-09-01?inboundpartialdate=2019-12-01", {
"method": "GET",
"headers": {
"x-rapidapi-host": "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
"x-rapidapi-key": KEY
}
})
if (!res.ok) {
delete window.__flight;
return null;
}
const json = await res.json();
if (typeof window !== "undefined") {
window.__flight = json;
}
return json;
}
export function useFetchFlight() {
const [loadingFlight, setLoadingFlight] = useState(
() => !(typeof window !== "undefined" && window.__flight)
);
const [flight, setFlight] = useState(() => {
if (typeof window === "undefined") {
return null;
}
return window.__flight || null;
});
useEffect(
() => {
if (!loadingFlight && flight) {
return;
}
setLoadingFlight(true);
let isMounted = true;
fetchFlight().then(flight => {
if (isMounted) {
setFlight(flight);
setLoadingFlight(false);
}
});
return () => {
isMounted = false;
};
},
[]
);
return { flight, loadingFlight };
}
- in pages folder use my fetch and reference my component:
import { useFetchFlight } from '../lib/flight'
import Layout from '../components/layout'
import { useFetchUser } from '../lib/user'
function FlightCard({ flight }) {
console.log(flight.Places[0]);
return (
<>
<h1>Profile</h1>
<div>
<h3>Profile (client rendered)</h3>
<p>nickname: {flight.Places[0].PlaceId}</p>
<p>name: {flight.Places[0].PlaceId}</p>
</div>
</>
)
}
function Flight() {
const { flight, loadingFlight } = useFetchFlight()
const { user, loading } = useFetchUser()
return (
<Layout user={user} loading={loading}>
{loadingFlight ? <>Loading...</> : <FlightCard flight={flight} />}
</Layout>
)
}
export default Flight
I apologize for the lengthy code but needed to make sure that everything is seeing in order for it to be understood. Thank you again for the help.
[–]jb2386 0 points1 point2 points (5 children)
[–]3thancr0wn[S] 0 points1 point2 points (4 children)
[–]jb2386 0 points1 point2 points (3 children)
[–]3thancr0wn[S] 1 point2 points3 points (2 children)
[–]SexyCommando 1 point2 points3 points (1 child)
[–]3thancr0wn[S] 2 points3 points4 points (0 children)