Below is my parent component which handles the state of the app.
function App() {
const [teamData, setTeamData] = useState<any | undefined>(undefined);
return(
<>
<Teams teams={teams} onTeamClick={() => setTeamData(team)} />
<Modal teamData={teamData} />
</>
)
}
Next is my component which passes the state back up the tree. However, I am getting the following typescript error:
Cannot invoke an object which is possibly 'undefined'.
type Props = {
teams: any[];
onTeamClick: () => void;
}
function Teams({teams, onTeamClick}: Props) {
return(
<div>
{teams.map(team => {
// the error is coming from onTeamClick
// No error if I change it to onTeamClick={onTeamClick}
<Team name={team.name} onTeamClick={() => onTeamClick(team)} />
})
</div>
)
}