How to build a React Video Modal with Hooks

I want to share with other Front End developers how to build a React Video modal from scratch.

I hope everyone finds this small tutorial really helpful.

I’m leaving the url of the working modal and code at the end of the tutorial if you want to test …


This content originally appeared on DEV Community and was authored by cesaruseche18

I want to share with other Front End developers how to build a React Video modal from scratch.

I hope everyone finds this small tutorial really helpful.

I'm leaving the url of the working modal and code at the end of the tutorial if you want to test it and and check out the code

First, we need to use the useState Hook to change the state of the modal once the user clicks the modal button and clicks the close modal button.

Second, we will do the same to build a loader icon once the modal is open and waits to fetch the video from YouTube like so.

const [modal, setModal] = useState(false);
  const [videoLoading, setVideoLoading] = useState(true);

  const openModal = () => {
    setModal(!modal);
  };

  const spinner = () => {
    setVideoLoading(!videoLoading);
  };

Third, we will start working on the JSX part of the code setting up an onClick event to the button and using the ternary operator for the modal and the loader icon like so.

return (
    <div className="App">
      <button onClick={openModal} className="">
        Click Me!
        {modal ? (
          <section className="modal__bg">
            <div className="modal__align">
              <div className="modal__content" modal={modal}>
                <IoCloseOutline
                  className="modal__close"
                  arial-label="Close modal"
                  onClick={setModal}
                />
                <div className="modal__video-align">
                  {videoLoading ? (
                    <div className="modal__spinner">
                      <BiLoaderAlt
                        className="modal__spinner-style"
                        fadeIn="none"
                      />
                    </div>
                  ) : null}
                  <iframe
                    className="modal__video-style"
                    onLoad={spinner}
                    loading="lazy"
                    width="800"
                    height="500"
                    src="https://www.youtube.com/embed/4UZrsTqkcW4"
                    title="YouTube video player"
                    frameBorder="0"
                    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
                    allowfullscreen
                  ></iframe>
                </div>
              </div>
            </div>
          </section>
        ) : null}
      </button>
    </div>
  );

Here I'm leaving the url of the code if you also want to check out the CSS styles and also the live demo.

Code: https://codesandbox.io/s/nkwxb?file=/src/App.js:423-1898
Live Demo: https://nkwxb.csb.app/

I hope I'm able to help anyone that wants to build a React Modal Video from scratch without using any library.

Follow me on Github & Connect with me on LinkedIn

https://github.com/cesareuseche
https://www.linkedin.com/in/cesar-useche-profile/


This content originally appeared on DEV Community and was authored by cesaruseche18


Print Share Comment Cite Upload Translate Updates
APA

cesaruseche18 | Sciencx (2021-09-15T23:14:29+00:00) How to build a React Video Modal with Hooks. Retrieved from https://www.scien.cx/2021/09/15/how-to-build-a-react-video-modal-with-hooks/

MLA
" » How to build a React Video Modal with Hooks." cesaruseche18 | Sciencx - Wednesday September 15, 2021, https://www.scien.cx/2021/09/15/how-to-build-a-react-video-modal-with-hooks/
HARVARD
cesaruseche18 | Sciencx Wednesday September 15, 2021 » How to build a React Video Modal with Hooks., viewed ,<https://www.scien.cx/2021/09/15/how-to-build-a-react-video-modal-with-hooks/>
VANCOUVER
cesaruseche18 | Sciencx - » How to build a React Video Modal with Hooks. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/15/how-to-build-a-react-video-modal-with-hooks/
CHICAGO
" » How to build a React Video Modal with Hooks." cesaruseche18 | Sciencx - Accessed . https://www.scien.cx/2021/09/15/how-to-build-a-react-video-modal-with-hooks/
IEEE
" » How to build a React Video Modal with Hooks." cesaruseche18 | Sciencx [Online]. Available: https://www.scien.cx/2021/09/15/how-to-build-a-react-video-modal-with-hooks/. [Accessed: ]
rf:citation
» How to build a React Video Modal with Hooks | cesaruseche18 | Sciencx | https://www.scien.cx/2021/09/15/how-to-build-a-react-video-modal-with-hooks/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.