I am working on a popup component where the parent component has the following code
import * as React from "react";
import "./styles.css";
import Popup from "./Popup";
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<Popup
src="https://picsum.photos/200"
alt="Fog"
coreValue="Customer Focus"
/>
<Popup
src="https://picsum.photos/200/300?grayscale"
alt="Grey Mountains"
coreValue="Persistence"
/>
</div>
);
}
The child component should render a custom popup dependent on the props
import * as React from "react";
import { useState } from "react";
interface Props {
src: string;
alt: string;
coreValue: string;
}
export default function Popup({ src, alt, coreValue }: Props): JSX.Element {
const [customerFocus, setCustomerFocus] = useState(false);
const [persistence, setPersistence] = useState(false);
const toggleCustomerFocus = () => {
setCustomerFocus(!customerFocus);
};
const togglePersistence = () => {
setPersistence(!persistence);
};
const data = [
{
customerFocus: {
title: "Dummy Title 1",
content: "Dummy Content 1"
},
persistence: {
title: "Dummy Title 2",
content: "Dummy Content 2"
}
}
];
return (
<>
<figure onClick={toggleCustomerFocus}>
<img src={src} alt={alt} />
<figcaption>{coreValue}</figcaption>
</figure>
{customerFocus && (
<section>
<h4>{data[0].customerFocus.title}</h4>
<p>{data[0].customerFocus.content}</p>
<button onClick={toggleCustomerFocus}>Close 1</button>
</section>
)}
{persistence && (
<section>
<h4>{data[1].persistence.title}</h4>
<p>{data[1].persistence.content}</p>
<button onClick={togglePersistence}>Close 2</button>
</section>
)}
</>
);
}
Where <figure onClick={toggleCustomerFocus}>, I would like to pass a custom onClick function from the parent to its child so that the first popup renders Dummy Title 1 and Dummy Content 1 when clicked and the second popup renders Dummy Title 2 and Dummy Content 2 when clicked. As of now, both render the same popup when clicked.
How do I send a custom onClick function from the parent to child?