I'm trying to use the useState hook for deciding whether a modal opens or not.
I have this in the parent component.
const handleState = (e) => {
setModalOpen(e);
};
const [ modalOpen, setModalOpen ] = useState(true);
and I am currently passing the hook as follows
return (
<ListWrapper>
{therapies.map((e, i) => {
return (
<div ref={(e) => (ref.current[i] = e)}>
<CardVertical
title={e.title}
info={e.info}
img={e.img}
imgAlt={e.imgAlt}
functions={[ modalOpen, handleState ]}
/>
</div>
);
})}
<Modal
img={therapies[0].img}
imgAlt={therapies[0].imgAlt}
title={therapies[0].title}
info={therapies[0].info}
functions={[ modalOpen, handleState ]}
/>
</ListWrapper>
);
In the modal I am getting and using the hook like this.
const Modal = ({ img, imgAlt, title, info, functions }) => {
const [ open, setOpen ] = functions;
<Button
onClick={() => {
if (functions.modalOpen) {
setOpen(false);
}
}}
I can read the open fine. But I cant seem to call the function.
My idea is that I'll change the information on the modal depending on what element is clicked in the array of elements. Probably by passing more state between the elements.
I'm thinking about maybe using context instead since somehow this feels like it's getting complicated.