I am fetching data in the server side using getServerSideProps. the data fetching is working well. I want to pass the fetched data to a component called UserForm. This is my code
// This gets called on every request
export async function getServerSideProps(context) {
// Fetch data from external API
const { id } = context.query;
const res = await fetch(`http://localhost:3001/v1/firm/${id}`)
const data = await res.json()
// Pass data to the page via props
return { props: { data } }
}
function ComingSoon({ data }) {
const router = useRouter()
const { id } = router.query
return (
<div className="coming-soon-area">
<TopHeader />
<NavbarStyleFive />
<UserForm data = {data}/>
<Footer />
</div>
)
}
export default ComingSoon;
And this is the content of UserForm
import React from 'react';
const UserForm = (data) => {
return (
<div className="coming-soon-area">
<h2>{data[0].name} coming soon</h2>
</div>
)
}
export default UsernForm;
I get an error in UserForm telling me that data is undefined. Any idea about the problem?
EDIT: Data is not undefined, this code works fine. I think the problem is accessing the data props in UserForm
<div className="coming-soon-area">
<TopHeader />
<NavbarStyleFive />
<h2>{data[0].name} coming soon</h2>
<Footer />
</div>