unique gf
unique gf

Reputation: 21

how do i pass props a react bootstrap button onClick function to anther component

const [modalShow, setModalShow] = React.useState(false);

return (
    <> 
        <Button variant="primary" onClick={() => setModalShow(true)}> Launch vertically centered modal
        <MyVerticallyCenteredModal
            show={modalShow}
            onHide={() => setModalShow(false)}
        />
    </>
)

Upvotes: 1

Views: 252

Answers (1)

Arash Ghazi
Arash Ghazi

Reputation: 991

If I understand correctly, you can pass a method to child component with props like this :

const TopComponent=()=>{
const [modalShow, setModalShow] = React.useState(false);
const MethodA=()=>{
   setModalShow(false)
}
return (
    <div>
      <Button onClick={MethodA}/>
      <MyVerticallyCenteredModal onClick={MethodA} />
    </div>
  );
};
const MyVerticallyCenteredModal = ({onClick}) => {
  return (
    <div>
      <Button onClick={onClick}/>
    </div>
  );
};

Upvotes: 1

Related Questions