I've got a grid of components rendered on a page using the map function and I want to only render the component with all it's details once I click on it and only manage to get an error saying:
Uncaught TypeError: Cannot read properties of undefined (reading 'logo')
I'm new to React and can't figure out why this is happening.
Some help would be much appreciated!
Here's my code:
Body component - (parent component)
import { useState } from "react";
import CompanyList from "./CompanyList";
import Company from "./Company";
const Body = ({ companies }) => {
const [viewCompany, setViewCompany] = useState(false);
const showCompanyHandler = (company) => {
console.log("Clicked on company card");
setViewCompany(true);
};
return (
<div className="appBody">
<CompanyList
companies={companies}
showCompanyHandler={showCompanyHandler}
/>
{viewCompany && <Company />}
</div>
);
};
export default Body;
CompanyList component (child of Body)
import Company from "./Company";
const CompanyList = ({ companies, showCompanyHandler }) => {
return (
<div className="companyList">
{companies.map((company) => (
<Company
key={company.id}
company={company}
showCompanyHandler={showCompanyHandler}
/>
))}
</div>
);
};
export default CompanyList;
Company component (child of CompanyList) - Component I want to display on the page
const Company = ({ company, showCompanyHandler }) => {
return (
<div className="company" onClick={(company) => showCompanyHandler(company)}>
{console.log(company)}
<img src={company.logo} alt="logo" />
<h1>{company.name}</h1>
<p>{company.companyDescription}</p>
</div>
);
};
export default Company;
I'm not sure what I'm missing here...