React Modal Tutorial Using Hooks and Styled Components

Поделиться
HTML-код
  • Опубликовано: 6 июн 2024
  • Learn how to make a simple React Modal Popup in this beginner React JS Tutorial. We will use React Hooks and styled components to build this project. You will be able to click outside of the modal and press the ESC key to close it as well.
    If you want to follow me along my coding journey, be sure to subscribe :)
    / @briandesign
    Timeline
    0:00 What we are building
    0:56 Creating React App
    3:32 Installing Dependencies
    5:00 Creating Modal Button
    18:52 Closing Modal on Outside Click
    Source code
    www.codavilla.com/posts/react...

Комментарии • 133

  • @ArcherJ1
    @ArcherJ1 2 года назад +2

    Love the way you built this. It leaves room to create more than one modal and style it easily. The only thing I'm having trouble with is getting it to render with portal.

  • @jiwanjeon3319
    @jiwanjeon3319 2 года назад

    This is a very good tutorial for beginners! I really appreciate your effort!!!

  • @ansulp5799
    @ansulp5799 3 года назад

    Thanks Brian, awesome work. Looking forward for more React videos 👍

  • @joza5422
    @joza5422 2 года назад +2

    2 days... i tell you 2 days I was trying to figure out how to do this and this solved my problem thank you Brian fr fr you're the truth

  • @dearesthui
    @dearesthui 2 года назад

    Thanks for this tutorial. Really helpful for what I need. Keep up the good work.

  • @anuragk6722
    @anuragk6722 2 года назад

    Amazing tutorial. absolutely loved it.

  •  2 года назад

    Thank you for the clean and the practical solution!

  • @sthefanocarvalho2823
    @sthefanocarvalho2823 2 года назад

    Awesome. Solved my problem. Thanks again, Brian

  • @rohitagni
    @rohitagni 2 года назад

    This is just what I wanted. Cheers man !

  • @earthycoin
    @earthycoin 3 года назад +5

    Brother Brian is at well-deserved 14k already xD! Love this vid

  • @ikmalshahzan2480
    @ikmalshahzan2480 3 года назад +4

    Thanks brian ! I really need this on my project

  • @valbonite5389
    @valbonite5389 3 года назад +4

    Damn I was just looking for modal tutorials for my React portfolio. Brian clutch!!!

  • @georgemoayi
    @georgemoayi 3 года назад

    Ah yes we already kno Issa hit. Preciate da vid Brian I needed it

  • @rachelperkins5874
    @rachelperkins5874 3 года назад +12

    I'm only 5 months into coding and 1 month into building in React and I was able to create a modal thanks to you! Never knew you could create styled components like that! All my CSS lives on css files. I'm going to attempt writing it on the component itself on my next project! Your code looks so clean!

    • @bgeorgiev87
      @bgeorgiev87 2 года назад

      Having in mind that this is posted 4 months ago ... my advice to anyone who reads this comment is that 5 months of coding are not nearly enough to start with a framework, unless you are some sort of genious. Most ppl wouldn't even understand the main concepts of JS (or programming in general) that early in their career. Take it slow, learn core JS ES6 syntax and after that go ahead with your first library. Otherwise I highly doubt you'll be able to even understand what is what.
      In general this is a really missleading comment for any rookie, but that's just my opinion.
      Other than that - nice tutorial :) Thank you!
      All the best

    • @rachelperkins5874
      @rachelperkins5874 2 года назад +3

      @@bgeorgiev87 Started on freecodecamp end of September (granted i built static html css sites in high school for funsies). Opened a GitHub account September 23, first commit January 6, hired as a full time front end engineer contractor April 1, switched to full time software engineer employee for the company June 1. So yes, it can be done.
      Almost all my work is react and, yes, I cried, multiple times. But if you literally quit your job to spend months coding, you can become a front end/full stack engineer. I also live with an engineer, dropped a pretty penny on a bootcamp, have a masters in teaching, and taught math. So yeah, lots of advantages. But it can be done! Don’t give up!

    • @ozzyfromspace
      @ozzyfromspace 2 года назад

      @@rachelperkins5874 congratulations! You're my inspiration. I started my job search a few days ago (ironically also a trained engineer/mathematician, specialized in the spectral analysis of pdes, particularly numerical methods using Fourier and Laplace analysis to study hyperbolic systems). Hoping to get my first web-dev job before years end.

    • @rachelperkins5874
      @rachelperkins5874 2 года назад

      You can do it! At this stage, you should be doing 1-2 interview problems (LeetCode), studying up data structures, and searching for opportunities (either cold or in your network) daily! Hope you have a cool project or two deployed! Best of luck! It can be done!

    • @LarsRyeJeppesen
      @LarsRyeJeppesen 2 года назад

      React is great for super beginners. Less so for experienced ...

  • @tinalee8686
    @tinalee8686 2 года назад

    Thanks you! Helped a lot

  • @damislav
    @damislav 3 года назад

    Thank you. Very easy to understand

  • @80Vikram
    @80Vikram 3 года назад

    Thanks for your awesome efforts, kindly take care of background noise. God bless you man

  • @chalermkhwannasritha7431
    @chalermkhwannasritha7431 3 года назад

    yayyyy, thanks Brian!

  • @lucasbertoldo5062
    @lucasbertoldo5062 2 года назад

    Thank you very much, man! You saved me

  • @ebjrtux
    @ebjrtux 3 года назад

    Congrats, great job!!!

  • @charliebrewer1668
    @charliebrewer1668 2 года назад

    Thanks again!!!

  • @ZEESHANJUNAID2222
    @ZEESHANJUNAID2222 3 года назад +2

    Looks great. I have used react-modal with one of my nextJS project and the only thing i had to figure out was the animation on modal close. I think there's props named closeTimeoutMS which allow modal close delay and we can add animations when modal closes.
    Nice work though :)

    • @briandesign
      @briandesign  3 года назад

      nice! yeah I wanted to make one from scratch, but react-modal pretty much does the same thing

  • @InverserPro
    @InverserPro 2 года назад

    Thank U, bro!

  • @pawels8040
    @pawels8040 3 года назад

    Very nice , thanks mate !!

  • @Krentiles
    @Krentiles 2 года назад

    Thank you!

  • @edmundodominguezagurcia7254
    @edmundodominguezagurcia7254 3 года назад

    Excellent video!

  • @aileen55
    @aileen55 2 года назад +1

    You're so entertaining to watch hahaha

  • @meredithjones7534
    @meredithjones7534 3 года назад +1

    Thanks for the awesome tutorials! They have been a huge help building up my portfolio!
    I hit a blocker on this one though, no matter what I've tried I cannot install the react-spring package. I use npm and tried what you did in the video, tried installing globally, and tried installing --save with no luck.
    Any suggestions for a workaround?

  • @wijaksanapanji
    @wijaksanapanji 3 года назад +1

    Love it❤️

  • @MaltonCanada
    @MaltonCanada 2 года назад

    Thanks brother

  • @JoaomdvFerreira
    @JoaomdvFerreira 3 года назад

    Another smooth video man! Clean and simple just how we like it. A quick question regarding the styled components, as fine as they look like and the perks that comes within, they have a gigantic downside for me, browser console debugging. I've found a way to give them the classes that i want but it feels like an unnecessary use of code lines. Just wanted to know from your experience what's actually worth more in the long term: Styled Components or Normal Html?

    • @briandesign
      @briandesign  3 года назад

      depends how simple your project is. You could try coding it up with regular html and css, then convert it into styled components after if it's hard to tell what is what

  • @Dzirmal
    @Dzirmal 3 года назад

    Thanks, Great Job.!!!

  • @infoberitabola7826
    @infoberitabola7826 3 года назад

    Nice tutorial !!! Greeting from Indonesia

  • @sarangkumar3803
    @sarangkumar3803 3 года назад +1

    Can you explain, Why you used useCallback hook and its reference in useEffect hook as a dependency? Thanks in advance.

  • @surendrareddyseelam9757
    @surendrareddyseelam9757 3 года назад

    You are awesome dude

  • @litsoschastlivyy7002
    @litsoschastlivyy7002 3 года назад

    hey man sweet video could you do one on how to link your modal to a table

  • @Arundhativlog
    @Arundhativlog 2 года назад

    I used this pop-up and I want to remove body scroll when pop-up is opened, can You please help?

  • @jonathanm.5813
    @jonathanm.5813 2 года назад

    Thanks, I tried with NextJS ton include modal animation but useSpring and animated doesn't work for me. Any tip to adapt it ?

  • @denisvradiy8503
    @denisvradiy8503 3 года назад +1

    nice video! please do more react videos)

    • @briandesign
      @briandesign  3 года назад

      I got a new react website vid dropping tomorrow

  • @tonymathew9469
    @tonymathew9469 2 года назад +1

    Hi @Brian, great tutorial! Could you also tell us how we could make this responsive for smaller screens? It would really help, thanks!

    • @raphaeltorsu8655
      @raphaeltorsu8655 2 года назад

      I think with a few media queries, it should be plausible

  • @rayvincentdelacruz8251
    @rayvincentdelacruz8251 3 года назад

    Nice bruh!

  • @dhimasdhias22
    @dhimasdhias22 3 года назад

    another cool tutorial, thanks, Brian!
    anyway, would you consider doing a Contact Form using React and Firebase? :D

  • @alekseytrump1586
    @alekseytrump1586 3 года назад

    Super Dev!))))

  • @jhonvaron1721
    @jhonvaron1721 2 года назад

    Hello friend, how do you make the stylecomponent code look like this for you and not like it looks like a string for me?

  • @rangabharath4253
    @rangabharath4253 3 года назад

    awesome :)

  • @alexgreen3630
    @alexgreen3630 3 года назад

    Thanks bro

  • @lennygonzalez2293
    @lennygonzalez2293 3 года назад

    Awesome Videos!! I was wondering if you could include display content over react-icons (using styled components) in one of your tutorials??

    • @briandesign
      @briandesign  3 года назад

      display content over react icons? I'm not sure what you mean

    • @lennygonzalez2293
      @lennygonzalez2293 3 года назад

      @@briandesign Sorry for explaining it poorly... I meant when you hover over an icon and display text. Also thank you for responding thats awesome

  • @synaxreader9326
    @synaxreader9326 Год назад

    On @21:13, I got an error in my console stating: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:....etc. I am buffled as to the reason why? Do you have any suggestions?

  • @ememobongumoren
    @ememobongumoren 3 года назад

    I know it's been three months since you uploaded the video, very nice tbh!!. But what how do i go about a displaying a modal popup on site load using react

  • @swevos7632
    @swevos7632 2 года назад

    Hello, what do you use as extension to have this kind of colors ?

  • @longbka
    @longbka 3 года назад +2

    Can you make these tutorials an dashboard admin with styled component

  • @Merig93
    @Merig93 3 года назад

    You could render the modal conditionally directly in App, by doing {showModal && }.
    This way you dont need to pass showModal as a prop and it's cleaner imo.

    • @briandesign
      @briandesign  3 года назад +3

      that works too! you could also just add onClick={e => e.stopPropagation()} on the Modal div and remove the useRef completely and it still would work. I just wanted to show useRef for this example

  • @markaguilar736
    @markaguilar736 3 года назад

    very usefull

  • @RemsterHD
    @RemsterHD 3 года назад

    Awesome tutorial! Would you consider doing something similar but when you click on the modal you are able to switch between photos?

    • @briandesign
      @briandesign  3 года назад +1

      like a photo gallery?

    • @RemsterHD
      @RemsterHD 3 года назад

      @@briandesign Yeah I was thinking where you have a project modal, once hovered, it will show the "learn more" then you click on it then the modal will open and then within that modal it would have some details about the photo you are looking at. Also give the user the ability to click through the photos and then depending on the photo the user is viewing the text/details of the photo change as well. This is defiantly more advanced but would be awesome to see!

  • @aistetamosauskiene1234
    @aistetamosauskiene1234 3 года назад

    Thank you for the video. Have a question: Modal component has showModal and setShowModal props, what are the types of those props?

    • @jabulaniemmanuelshili505
      @jabulaniemmanuelshili505 3 года назад

      my guess would be one is a boolean and one is a function , as in showModal is a boolean and setShowModal is a function.

  • @hariace08
    @hariace08 2 года назад

    How can we trigger the modal without a Button? For example every time a user logs in, a model appears after few sec on the landing page .

  • @dixondeuxyeux
    @dixondeuxyeux 3 года назад

    Hello Brian, get tutorial. I have used modals in html projects. This looks far more difficult to code and I also wanted to ask how does one use multiple modals with different images and text on a page?? Many thanks

  • @therevenant9296
    @therevenant9296 2 года назад

    I need to implement this in my Navbar. I tried and the modal seems to misalign from page.

  • @eddyed3212
    @eddyed3212 3 года назад

    what if this modal should updated the environment that it is triggered from, then it will make the modal close, and it is not the behaviour that you may want

  • @rahulvirat7859
    @rahulvirat7859 3 года назад

    Nice bro

  • @goresometimes3528
    @goresometimes3528 3 года назад

    Not sure if anyone will see this but I did this tutorial and the image wasn't rendering and I'm not sure what I missed. I followed it to a T and couldn't figure out why the image was broken. Would appreciate any help. Thank you.

  • @EzeJ99
    @EzeJ99 3 года назад

    I love you so much man (no homo), thanks to you i can finish my personal website. Greetings from Argentina!

  • @mohammedirfan-us6cf
    @mohammedirfan-us6cf 3 года назад

    Big fan ...love from india

  • @makak360
    @makak360 3 года назад

    Hy dudes , for me img don't display with relative or absolute path , anyone have an idea ?

  • @PwnUrBadCock
    @PwnUrBadCock 3 года назад

    Big thanks for this video man.
    However, I faced one issue while working on my project. Inside of return function I have a map method, which takes a lot of pokemon cards, each card has a learn more button. The idea was to create a Modal which will be individual for each pokemon card. However, when I click learn more button, Modal opens up and always showing the very last pokemon information. Probably because initial render occurs instantly after the npm start, and all modals are taking the very last item from the map.
    Can you please make a tutorial on how to use Modal inside of the map? Maybe internet shop with different items where each item has it's own Modal. That would be awesome!
    Cheers!

    • @briandesign
      @briandesign  3 года назад

      yeah I can make one in the future. It sounds like an image gallery design would replicate what you're trying to do. Basically you click on an image and it only shows that specific one. I made something similar with regular javascript, but I'd have to refactor it in react

    • @PwnUrBadCock
      @PwnUrBadCock 3 года назад

      @@briandesign That's a big hint! I will try to Google image galleries powered by react.

  • @minhtriha1222
    @minhtriha1222 3 года назад +1

    Thank Brian. Well done!
    But I still stuck at closing animation( the modal flow up then disappear). Can you help me and everybody here to solve this

    • @briandesign
      @briandesign  3 года назад

      are there any errors in your console? or do you mean the animation doesn't work at all?

    • @minhtriha1222
      @minhtriha1222 3 года назад

      @@briandesign Your code works well. I mean an additional animation when closing the Modal. In fact, the Modal immediately disappears when showModal: false. Would you consider adding a disappear animation for the Modal. I mean the Modal floats from middle to top of the page(opposite direction of showing Modal). I tried to add some css animation when showModal: false but the state immediately sets false and no animation happens, just disappear.

    • @colorfilter2046
      @colorfilter2046 3 года назад

      @@minhtriha1222 I'm thinking about the same problem. Have you found a solution? If so, please share it with me.

  • @sivaganesh4489
    @sivaganesh4489 3 года назад

    pls do more videos on react design

  • @yuricavalini3718
    @yuricavalini3718 3 года назад

    Hello, Brian! First, thank you for this tutorial! I really enjoy your channel.
    I'm getting warning message in the console about memory leak: "Warning: Can't perform a React state update on an unmounted component..."
    I tried to solve this, but didn't figure out how to handle this yet. As I can see you tried to manage it by passing a return statement in the useEffect. Are you or anyone else having this issue?

    • @briandesign
      @briandesign  3 года назад

      check my github code and see if you mistyped something

    • @yuricavalini3718
      @yuricavalini3718 3 года назад

      @@briandesign I did. Seems like it's an issue because of react's version and react-spring. Still don't know why this is happening, but removing the element solved the issue. Thank you.

  • @shubhammetkar6021
    @shubhammetkar6021 3 года назад +1

    Is it responsive?

  • @80Vikram
    @80Vikram 3 года назад

    Nothing is UP in morning time, only after 21:00

  • @saivivekravi4979
    @saivivekravi4979 3 года назад +3

    Can you make these toutorials with materialui as many are waiting for this

  • @shayanjafari1935
    @shayanjafari1935 3 года назад

    hello when i use the styled component they show green and the snippet is off what should i do??????

    • @briandesign
      @briandesign  3 года назад

      add the extension vscode-styled-components

  • @papichongogamer6144
    @papichongogamer6144 2 года назад

    Great! Only you mist hover function 🤭

  • @mbilal02
    @mbilal02 3 года назад

    Hello Respected Sir, Please make a water ripple effect on the background banner/header/hero image video in react js please sir

  • @Nathan-pu9um
    @Nathan-pu9um 3 года назад

    just wandering how to stop scrolling if say you clicked the modal half way down the page in another section

    • @sreekar_s
      @sreekar_s 3 года назад

      On launching the modal, execute this:
      document.body.style.overflow="hidden"
      On close, execute this:
      document.body.style.overflow="visible"
      Hope this is what you are looking for

    • @arundhati7163
      @arundhati7163 2 года назад +1

      @@sreekar_s Can you pls share the short video On launching the modal, execute this:
      document.body.style.overflow="hidden"
      On close, execute this:
      document.body.style.overflow="visible"

  • @soniakashyap6261
    @soniakashyap6261 3 года назад

    My Img is not showing can u tell me where is the prob

    • @damislav
      @damislav 3 года назад

      add .default after brackets

  • @vijuvaghish2592
    @vijuvaghish2592 3 года назад

    why you use require for img in modal.

    • @briandesign
      @briandesign  3 года назад

      react has issues with regular imports, so I use require to get the image. If that doesn't work, you can always go to the top of file and import {Image} from './modal.jpg' then pass 'Image' for the src like and that should work as well.
      If you just search on stackoverflow you should see a bunch of examples using the import {Image} from 'imagelink'. There's a few alternatives as well, but this should work. If not let me know

  • @dhanushr6172
    @dhanushr6172 3 года назад

    Is that modal responsive?

    • @briandesign
      @briandesign  3 года назад

      this just shows the open/close functionality

    • @dhanushr6172
      @dhanushr6172 3 года назад

      @@briandesign thank you

  • @JounitoJons
    @JounitoJons 3 года назад

    Portals?

    • @briandesign
      @briandesign  3 года назад +1

      yeah you could use portals too, but I wanted to keep this template simple so people can refactor and add extra things

  • @LarsRyeJeppesen
    @LarsRyeJeppesen 2 года назад

    This is not lazy loaded.