C
C
connecter2019-11-06 13:00:10
React
connecter, 2019-11-06 13:00:10

Logic when creating a modal window?

Hello everyone, tell me the logic of how to open / close a modal window when:
1. Opens when a button-component (Button) is clicked in another component (Main)
2. Closes when a regular div is clicked in the modal window component itself (Modal)
Accordingly, there is a Main, it has a Button and a Modal

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan V, 2019-11-06
@connecter

In Main, add a state, for example, "show-modal" with a value of false by default.
Also in Main you make a function, for example, "switchModal" in which you do setState({ "show-modal": !this.state."show-modal"}). On the Button you put "switchModal" on click.
Show modal depending on the state. Type In the Modal itself, pass props with the "switchModal" function. Inside Modal you make a function Hang "closeModal" on the element you want to close the Modal on click.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question