Issue as stated in topic. Goal is to open a modal, and use item.imdbID to fetch data. As it is now, a modal is opened for every entry in the table, and also the id prop is sent for every entry.
const { toggle, visible } = useModal();
<tbody>
{items.map((item, i) => (
<tr key={i}>
<MTd>{item.Title}</MTd>
<MTd>{item.Year}</MTd>
<MTd>{item.imdbID}</MTd>
<MTd>{item.Type}</MTd>
<MTd><button onClick={toggle}>Detaljer</button>
<DetailedViewModal visible={visible} toggle={toggle} id={item.imdbID} />
</MTd>
</tr>
))}
</tbody>
I know one can do <button onClick={e => testClick(item.imdbID)}>Details</button> when the function resides in the same file, but unsure on how to handle this with a modal.
The modal is custom made; useModal.js:
const useModal = () => {
const [visible, setVisible] = useState(false);
function toggle() {
setVisible(!visible);
}
return {toggle, visible}
};
Modal.js
const Modal = ({ visible, toggle, body }) => visible ? ReactDOM.createPortal(
<div className="modal">
<div className="modal-pop" role="dialog" aria-modal="true">
<div>{body}</div>
<button type="button" onClick={toggle}>Close</button>
</div>
<div className="modal-overlay"></div>
</div>, document.body
) : null;
DetailedViewModal.js
return (
<>
<Modal visible={visible} toggle={toggle} body={modalBody()} />
</>
)
Any good suggestions to make this work is much appreciated.