1

How to pass data from the first element to the second one by clicking the button?

If we open the second component by clicking on the panel, it must to stay empty.

It is welcomed if the answer is with items, and not with Collapse.Panel.

Also, I had to hardcode the activeKey value which is completely wrong and immobilizes the entire parent Collapse component.

interface ChildComponentProps {
 //activeKey: number;
 //setActiveKey: React.Dispatch<React.SetStateAction<string>>;
 setActiveKey: (newType: string) => void;
}
const Element1: React.FC<ChildComponentProps> = ({ setActiveKey }) => {
 // need to pass this 'car'
 const car: { type: string, model: string, year: number } = {
 type: "Toyota",
 model: "Corolla",
 year: 2009
 };
 const handleClick = (key: string) => {
 setActiveKey(key);
 }
 return (
 <div>
 <div>
 <Button
 onClick={() => handleClick('2')} // wrong hardcoding!
 >expandNextPanel
 </Button>
 </div>
 </div>
 );
}
const Element2 = () => {
 return (
 <div>
 <div>
 go back to the first component and click the button
 </div>
 </div>
 );
}
const CollapseComponent = () => {
 const [activeKey, setActiveKey] = useState<string>();
 return (
 <Collapse
 activeKey={activeKey}
 onChange={() => setActiveKey(activeKey)}
 >
 <Collapse.Panel header="Element1" key="1">
 <Element1
 setActiveKey={setActiveKey}
 />
 </Collapse.Panel>
 <Collapse.Panel header="Element2" key="2">
 <Element2 />
 </Collapse.Panel>
 </Collapse>
 );
}
export default CollapseComponent;
asked Jul 12, 2025 at 7:17

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.