P
P
PetyaSD2021-08-24 17:17:48
React
PetyaSD, 2021-08-24 17:17:48

How to make modal window component appear on button click in React?

There are 3 components. Parent component Container, it stores ContainerItems. When you click on the button inside the ContainerItem'a, a full-screen modal window should appear, where the ContainerItem's data will be transferred (Something like adding pizza on the DoDo website). How to implement it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexandroppolus, 2021-08-24
@PetyaSD

const Container = () => {
  const [shownItemId, setsShownItemId] = useState('');

.....
    <ContainerItem itemId={id} onShowDetails={() => setsShownItemId(id)} ... />
.....
  {shownItemId && <Modal itemId={shownItemId} ... />
};

The ContainerItem here is rendered in the map loop.
This cycle can be moved to a separate memo component (to which setsShownItemId should be passed to the props) so that all items are not re-rendered when the modal is opened / closed, but these are particulars.

R
Rostislav, 2021-08-24
@rostikowb

Well, for example, if I understood the question correctly , in general, you can use either, I often use this one https://material-ui.com/ru/components/modal/
{isStateTrue ? <Modal data={data}/>: null}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question