To understand how to conditionally render the contents of a modal and how to pass a function as props to a child component. Modals are pop-ups that are typically dynamically rendered.
In this lab, we will practice with using React to conditionally render elements on the page. We will need to pass props (both a state and a function) to do so.
Topics:
- React conditional rendering
- React props
Your work will result in:
- A webpage with a 'Open Modal' button.
- When the button is clicked, a semi-transparent modal pops up that covers the original content. It should inform the user it is a modal, and contain a 'Close Modal' button.
- When 'Close Modal' is clicked, the modal disappears and only the original content remains.
- Create a new file,
Modal.jsx
. Set upModal.jsx
as you would any other new component.
In App.jsx
- Ensure you have the Modal component imported.
- Create the
modalState
state variable and have it set initially tofalse
. - Create a function named
handleClick
. - Within
handleClick
, set up the following conditional logic: ifmodalState
is true,setModalState
to false. Else,setModalState
to true. - Now, in the return statement, set up the following:
- Within
<main>
, a button that has an onClick event set to thehandleClick
function and has the text "Open modal". - Below the button, a paragraph of placeholder text.
- Below the paragraph, render
<Modal />
. Pass it two props:modalState
andhandleClick
.
In Modal.jsx
- Ensure the Modal function accepts the parameter of
props
AND thatModal.css
is imported. - Inside of your Modal function, the first line will be a conditional if statement that checks if
props.modalState
is true. - Inside of the if statement's code block, set up a
return()
statement that contains the following: - A
<main>
element with an id ofmodal-background
. - A
<section>
element with an id ofmodal-content
; it should contain "I am a modal!" - A
<button>
within<section>
with theonClick
event ofprops.handleClick
and the text "Close modal." - Outside of the if statement's code block, set up a
return()
statement that contains the following: - A
<div>
with an inlinestyle
ofdisplay:"none"
.
In this lab, we will have created a page that contains buttons to open and close a modal.
The software should:
- Have placeholder text and an "Open Modal" button.
- When the "Open Modal" button is clicked, new elements appear on the page that inform the user that this is a modal; it also contains a "Close Modal" button.
- When the "Close Modal" button is clicked, the new elements disappear and the page returns to its original state.
- Many modals you come across on the Internet do not utilize a "Close" button but close when you click on the background. How could you implement an
onClick
event to do this? - Many modals you come across on the Internet pop up upon page load. How would you change the conditional logic and buttons to acheive this?
- This modal is not styled. Try incorporating this frontendmentor challenge so your modal is now an order summary. You will need to import the image assets and the fonts into replit.