React Modal Tutorial Using Hooks and Styled Components

  Рет қаралды 119,568

Brian Design

Brian Design

Күн бұрын

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 3 жыл бұрын
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.
@joza5422
@joza5422 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
@Azertex
@Azertex 2 жыл бұрын
me too...
@jiwanjeon3319
@jiwanjeon3319 2 жыл бұрын
This is a very good tutorial for beginners! I really appreciate your effort!!!
@dearesthui
@dearesthui 2 жыл бұрын
Thanks for this tutorial. Really helpful for what I need. Keep up the good work.
@ansulp5799
@ansulp5799 3 жыл бұрын
Thanks Brian, awesome work. Looking forward for more React videos 👍
@briandesign
@briandesign 3 жыл бұрын
Thanks Ansul!
@anuragk6722
@anuragk6722 3 жыл бұрын
Amazing tutorial. absolutely loved it.
@rachelperkins5874
@rachelperkins5874 3 жыл бұрын
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 3 жыл бұрын
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 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 ...
@rohitagni
@rohitagni 2 жыл бұрын
This is just what I wanted. Cheers man !
@earthycoin
@earthycoin 3 жыл бұрын
Brother Brian is at well-deserved 14k already xD! Love this vid
@briandesign
@briandesign 3 жыл бұрын
Appreciate it!
2 жыл бұрын
Thank you for the clean and the practical solution!
@valbonite5389
@valbonite5389 3 жыл бұрын
Damn I was just looking for modal tutorials for my React portfolio. Brian clutch!!!
@briandesign
@briandesign 3 жыл бұрын
aye thanks Val
@sthefanocarvalho2823
@sthefanocarvalho2823 2 жыл бұрын
Awesome. Solved my problem. Thanks again, Brian
@georgemoayi
@georgemoayi 3 жыл бұрын
Ah yes we already kno Issa hit. Preciate da vid Brian I needed it
@briandesign
@briandesign 3 жыл бұрын
awesome thanks!
@chalermkhwannasritha7431
@chalermkhwannasritha7431 3 жыл бұрын
yayyyy, thanks Brian!
@damislav
@damislav 3 жыл бұрын
Thank you. Very easy to understand
@ikmalshahzan2480
@ikmalshahzan2480 3 жыл бұрын
Thanks brian ! I really need this on my project
@briandesign
@briandesign 3 жыл бұрын
nice!
@tinalee8686
@tinalee8686 2 жыл бұрын
Thanks you! Helped a lot
@80Vikram
@80Vikram 3 жыл бұрын
Thanks for your awesome efforts, kindly take care of background noise. God bless you man
@lucasbertoldo5062
@lucasbertoldo5062 3 жыл бұрын
Thank you very much, man! You saved me
@ebjrtux
@ebjrtux 3 жыл бұрын
Congrats, great job!!!
@briandesign
@briandesign 3 жыл бұрын
Thanks Edson!
@charliebrewer1668
@charliebrewer1668 2 жыл бұрын
Thanks again!!!
@wijaksanapanji
@wijaksanapanji 3 жыл бұрын
Love it❤️
@briandesign
@briandesign 3 жыл бұрын
Thanks!
@pawels8040
@pawels8040 3 жыл бұрын
Very nice , thanks mate !!
@briandesign
@briandesign 3 жыл бұрын
Thanks Pawel!
@InverserPro
@InverserPro 2 жыл бұрын
Thank U, bro!
@aileen55
@aileen55 3 жыл бұрын
You're so entertaining to watch hahaha
@edmundodominguezagurcia7254
@edmundodominguezagurcia7254 3 жыл бұрын
Excellent video!
@briandesign
@briandesign 3 жыл бұрын
Thanks
@Krentiles
@Krentiles 2 жыл бұрын
Thank you!
@ZEESHANJUNAID2222
@ZEESHANJUNAID2222 3 жыл бұрын
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
@Dzirmal
@Dzirmal 3 жыл бұрын
Thanks, Great Job.!!!
@briandesign
@briandesign 3 жыл бұрын
Thanks!
@surendrareddyseelam9757
@surendrareddyseelam9757 3 жыл бұрын
You are awesome dude
@briandesign
@briandesign 3 жыл бұрын
Thanks!
@MaltonCanada
@MaltonCanada 2 жыл бұрын
Thanks brother
@rangabharath4253
@rangabharath4253 3 жыл бұрын
awesome :)
@briandesign
@briandesign 3 жыл бұрын
Thanks Ranga!
@alekseytrump1586
@alekseytrump1586 3 жыл бұрын
Super Dev!))))
@briandesign
@briandesign 3 жыл бұрын
Thanks
@rayvincentdelacruz8251
@rayvincentdelacruz8251 3 жыл бұрын
Nice bruh!
@briandesign
@briandesign 3 жыл бұрын
Thanks 🔥
@infoberitabola7826
@infoberitabola7826 3 жыл бұрын
Nice tutorial !!! Greeting from Indonesia
@briandesign
@briandesign 3 жыл бұрын
Thanks!
@meredithjones7534
@meredithjones7534 3 жыл бұрын
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?
@sarangkumar3803
@sarangkumar3803 3 жыл бұрын
Can you explain, Why you used useCallback hook and its reference in useEffect hook as a dependency? Thanks in advance.
@markaguilar736
@markaguilar736 3 жыл бұрын
very usefull
@alexgreen3630
@alexgreen3630 3 жыл бұрын
Thanks bro
@denisvradiy8503
@denisvradiy8503 3 жыл бұрын
nice video! please do more react videos)
@briandesign
@briandesign 3 жыл бұрын
I got a new react website vid dropping tomorrow
@jonathanm.5813
@jonathanm.5813 3 жыл бұрын
Thanks, I tried with NextJS ton include modal animation but useSpring and animated doesn't work for me. Any tip to adapt it ?
@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
@longbka
@longbka 3 жыл бұрын
Can you make these tutorials an dashboard admin with styled component
@litsoschastlivyy7002
@litsoschastlivyy7002 3 жыл бұрын
hey man sweet video could you do one on how to link your modal to a table
@hariace08
@hariace08 3 жыл бұрын
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 .
@makak360
@makak360 3 жыл бұрын
Hy dudes , for me img don't display with relative or absolute path , anyone have an idea ?
@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.
@swevos7632
@swevos7632 2 жыл бұрын
Hello, what do you use as extension to have this kind of colors ?
@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?
@EzeJ99
@EzeJ99 3 жыл бұрын
I love you so much man (no homo), thanks to you i can finish my personal website. Greetings from Argentina!
@tonymathew9469
@tonymathew9469 2 жыл бұрын
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
@rahulvirat7859
@rahulvirat7859 3 жыл бұрын
Nice bro
@briandesign
@briandesign 3 жыл бұрын
Thanks
@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
@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 жыл бұрын
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
@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
@mohammedirfan-us6cf
@mohammedirfan-us6cf 3 жыл бұрын
Big fan ...love from india
@briandesign
@briandesign 3 жыл бұрын
Thanks Mohammed!
@mohammedirfan-us6cf
@mohammedirfan-us6cf 3 жыл бұрын
@@briandesign 🙌
@synaxreader9326
@synaxreader9326 2 жыл бұрын
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?
@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.
@Arundhativlog
@Arundhativlog 2 жыл бұрын
I used this pop-up and I want to remove body scroll when pop-up is opened, can You please help?
@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
@therevenant9296
@therevenant9296 2 жыл бұрын
I need to implement this in my Navbar. I tried and the modal seems to misalign from page.
@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
@dhimasdhias22
@dhimasdhias22 3 жыл бұрын
another cool tutorial, thanks, Brian! anyway, would you consider doing a Contact Form using React and Firebase? :D
@briandesign
@briandesign 3 жыл бұрын
Possibly!
@dhimasdhias22
@dhimasdhias22 3 жыл бұрын
@@briandesign cant wait for it 🤣
@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
@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
@saivivekravi4979
@saivivekravi4979 3 жыл бұрын
Can you make these toutorials with materialui as many are waiting for this
@saivivekravi4979
@saivivekravi4979 3 жыл бұрын
Please reply
@sivaganesh4489
@sivaganesh4489 3 жыл бұрын
pls do more videos on react design
@briandesign
@briandesign 3 жыл бұрын
will do!
@minhtriha1222
@minhtriha1222 3 жыл бұрын
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.
@shubhammetkar6021
@shubhammetkar6021 3 жыл бұрын
Is it responsive?
@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 жыл бұрын
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!
@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 жыл бұрын
@@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"
@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.
@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.
@papichongogamer6144
@papichongogamer6144 2 жыл бұрын
Great! Only you mist hover function 🤭
@80Vikram
@80Vikram 3 жыл бұрын
Nothing is UP in morning time, only after 21:00
@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
@JounitoJons
@JounitoJons 3 жыл бұрын
Portals?
@briandesign
@briandesign 3 жыл бұрын
yeah you could use portals too, but I wanted to keep this template simple so people can refactor and add extra things
@dhanushr6172
@dhanushr6172 3 жыл бұрын
Is that modal responsive?
@briandesign
@briandesign 3 жыл бұрын
this just shows the open/close functionality
@dhanushr6172
@dhanushr6172 3 жыл бұрын
@@briandesign thank you
@LarsRyeJeppesen
@LarsRyeJeppesen 2 жыл бұрын
This is not lazy loaded.
Create react projects
22:33
Hitesh Choudhary
Рет қаралды 39 М.
React Navbar Tutorial Responsive - 3 Designs
34:09
Brian Design
Рет қаралды 258 М.
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 45 МЛН
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 33 МЛН
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 30 МЛН
Modal in ReactJS - Code a React Modal Tutorial using Hooks
18:26
React Todo List App Tutorial - Beginner React JS Project Using Hooks
50:31
Build any layout with tailwind | crash course
34:28
Hitesh Choudhary
Рет қаралды 59 М.
Modal in Next.js & React | Create a Dialog Component
22:57
Dave Gray
Рет қаралды 31 М.
Learn React Portal In 12 Minutes By Building A Modal
12:11
Web Dev Simplified
Рет қаралды 234 М.
Create a modal with React (Pop-up)
6:39
The Web School.
Рет қаралды 135 М.
The Story of Next.js
12:13
uidotdev
Рет қаралды 558 М.
Build A Custom React JS Modal - Using Hooks (Pop-up Window)
12:27
Code Commerce
Рет қаралды 57 М.