I have a parent element which passes its state value, down as props, into its Child component. Now, whenever I try to access the the state of the parent, in the child component(using props), the props value is undefined. (props of child is undefined when I pass it into useState(), but is defined when i console.log() it)
Here's the parent code:
function Parent() {
const [openModal, setOpenModal] = useState('close-modal')
const revealStats = (e) => {
e.preventDefault();
//open modal state
setOpenModal('open-modal')
};
return (
<>
<div className='player-wrapper' key={key} onClick={(event) => revealStats(event)}>
<ModalClubPages openModal={openModal}/>
</div>
</>
);
}
export default Parent;
Here's the childs code:
function ModalClubPages({openModal}) {
const [modalState,setModalState] = useState(openModal)
const closeModal = (e) =>{
e.preventDefault();
setModalState('close-modal')
}
return (
<div id={modalState}>
<button id="close" onClick={closeModal}> X </button>
</div>
)
}
export default ModalClubPages