Dark Theme in NextJs 13 - Using React Context in Server Components

Поділитися
Вставка
  • Опубліковано 28 лис 2024

КОМЕНТАРІ • 159

  • @ShahabSadeghi-z1d
    @ShahabSadeghi-z1d Місяць тому +1

    Hamed, it's truly a great pleasure learning from and watching your videos. I'm incredibly proud of you, man. You're doing something rare and special, and it really means a lot. Parcham balast! 🇮🇷
    Keep soaring high!"

    • @hamedbahram
      @hamedbahram  Місяць тому

      Eraadat Shahab jan 🫡 I appreciate that!

  • @outthereactive939
    @outthereactive939 Рік тому +1

    many thanks - have been trying (and failing) to get context to work with v13 all day. This is so great - thank you

    • @hamedbahram
      @hamedbahram  Рік тому +1

      Glad it helped! Appreciate your comment.

  • @huseyincancirkin
    @huseyincancirkin 9 місяців тому +1

    i cant explain how this video helpful. thank you mate!

  • @giovannitonussi3746
    @giovannitonussi3746 Рік тому

    Another great video with very recent NextJs features.

  • @yantaosong
    @yantaosong Рік тому +1

    really appreciate it , I met the same problem .

  • @caseyharper
    @caseyharper 5 місяців тому +1

    Firstly, thanks so much for the video! However, is there any possibility of doing this theme switcher on server components? My understanding is that client components aren't the best for SEO and if the entire website is wrapped in the client component for the theme, doesn't that mean the entire site isn't at all good for SEO?

    • @hamedbahram
      @hamedbahram  5 місяців тому

      First of all wrapping your app with a client component doesn't turn all into client components. You can pass server components as children to client components without affecting the boundary. Secondly, in NextJs client components are pre-rendered on the server and then hydrated on the client, which means your initial response has content as far as SEO is concerned, this is one of the reasons people use NextJs: for SEO purposes.

  • @Wahab00916
    @Wahab00916 Рік тому +1

    very helpful and easy explanation thanks

  • @horacioabitu
    @horacioabitu Рік тому

    is very clear, here we say "clearer add water" 😃 "mas claro hechale agua", thanks! 👍

  • @farhanramadhan5924
    @farhanramadhan5924 Рік тому +1

    Thank you hamed, its very useful

  • @matheusteixeira4161
    @matheusteixeira4161 Рік тому

    Thank, man! Very useful tutorial. Did this and worked perfectly for me! Cheers from Brazil

  • @0xtz_
    @0xtz_ Рік тому +1

    😮 amazing, keep going 👌🏼

  • @contact3311
    @contact3311 Рік тому +1

    Great video. Do you know how we should pass the children in the ThemeProvider on a tsx file?

    • @hamedbahram
      @hamedbahram  Рік тому

      Same way with the `children: React.ReactNode` type

  • @stevenrome6
    @stevenrome6 Рік тому +1

    Hi, Great video!
    When I use 'use client' for the ThemeProvider the children all become "client components"?

    • @hamedbahram
      @hamedbahram  Рік тому +1

      Good question! You cannot import server components in client components, but you can pass server components as props or children to a client component without turning them into a client component.

    • @oksanagez1169
      @oksanagez1169 8 днів тому

      @@hamedbahram But how will you read the theme in children?🤔 If you use a custom hook for the theme provider, you'll immediately make the page a client component (since hooks only work in 'use client'). I’m trying to find a solution and still can’t find one.🤷‍♀

  • @Shawn-Mosher
    @Shawn-Mosher Рік тому

    Just implimented this last week! Good to know I did it correctly. Great video as always and you are appreciated

  • @6qat
    @6qat Рік тому +3

    In the first solution, the error remains when you change to dark mode and then reload the page.

    • @hamedbahram
      @hamedbahram  Рік тому +1

      Thanks for sharing. That's a good point 🤔 In that case the `suppresshydrationwarning` is the way to go 🙂

  • @olagbegidamilola7633
    @olagbegidamilola7633 Рік тому +1

    Thank you!! this was so helpful

  • @sadoundhirat886
    @sadoundhirat886 Рік тому +4

    I'm following the next js series, may I ask if you can do RTL and Internationalization for EN and AR languages inside the app directory.
    Love your videos, keep going.

    • @hamedbahram
      @hamedbahram  Рік тому +1

      Great suggestion! I'll definitely cover that in future videos.

    • @hassanallen1823
      @hassanallen1823 Рік тому +1

      i just add

    • @hamedbahram
      @hamedbahram  Рік тому +1

      @@hassanallen1823 Absolutely! then you can use tailwind to apply `:rtl` modifier. Thanks for the comment Hassan!

    • @hassanallen1823
      @hassanallen1823 Рік тому +1

      @@hamedbahram you're welcome hamed jan love your videos 3>

    • @hamedbahram
      @hamedbahram  Рік тому +1

      @@hassanallen1823 eraadat 🫡

  • @Grishopping
    @Grishopping Рік тому

    Thank you Brother... att. Jose Grillo - Venezuela

  • @nifijoy6910
    @nifijoy6910 Рік тому +1

    Hi. can we use a lottie to switch the theme from dark to light and back on clicking the lottie. Can u help me

    • @hamedbahram
      @hamedbahram  Рік тому

      Hey! Yeah you can render them inside a button and use the `setTheme` function to toggle the theme when the button is clicked. Does that answer your question?

  • @asfnobambu
    @asfnobambu Рік тому +1

    This solution for the Hydration problem only work for dark/light. If you create an environment with custom themes, for example greenish, bluish and reddish nothing prevent the error... I'm using NextJS 3.4.8, next-themes 0.2.1 and Tailwind 3.3.2.

    • @hamedbahram
      @hamedbahram  Рік тому

      Did you try the `suppressHydrationWarning` prop on your `html` tag?

  • @agustinmolina1027
    @agustinmolina1027 Рік тому

    Good video! saludos desde argentina 😉

  • @hist-geo8489
    @hist-geo8489 Рік тому +1

    thank you for sharing i appreciate your help, can you plz explain how we can switch the svg logo color when the theme is changed ?

    • @hamedbahram
      @hamedbahram  Рік тому

      You can have a `color` or `fill` attribute on your `svg` element that controls the color or fill. I usually set that to "currentColor" like `color="currentColor" and then change the color on the parent of the svg depending on the theme, or you can set the hex color you want directly on the `svg` like `color="#fff"

    • @hist-geo8489
      @hist-geo8489 Рік тому +1

      thank you @@hamedbahram

    • @hamedbahram
      @hamedbahram  Рік тому

      @@hist-geo8489 you're welcome.

  • @youngslol
    @youngslol Рік тому +1

    Thanks for great tutorial video!! fyi, your course site's "order now" button has higher zindex than header navigation bar :)

    • @hamedbahram
      @hamedbahram  Рік тому +1

      You're right 😅 thanks for flagging that.

  • @imkir4n
    @imkir4n Рік тому

    Nice content hamed

  • @andru5054
    @andru5054 10 місяців тому +1

    Thanks for the good video

  • @luqmanusman70
    @luqmanusman70 Рік тому +1

    Hi, how can I use multiple themes like light, dark, custom1, custom2 with different color schemes. As you specified only two themes i.e. light and dark only, thanks

    • @hamedbahram
      @hamedbahram  Рік тому

      Good question! you can pass in multiple themes to the `ThemeProvider` component from `next-themes` something like this: ``. You can read more about this here: github.com/pacocoursey/next-themes#more-than-light-and-dark-mode
      You would then also need to add custom variants to Tailwind to support these added themes. You read about adding custom variants here: tailwindcss.com/docs/plugins#adding-variants

    • @asfnobambu
      @asfnobambu Рік тому +1

      Did you manage to create more than dark/light themes ? Today I finally did it but still that Hydration errors...

    • @hamedbahram
      @hamedbahram  Рік тому

      @@asfnobambu That's awesome! Do you mind creating a PR on my repo for it?

  • @mahdikoohneshin2763
    @mahdikoohneshin2763 Рік тому

    Is this package keep the user selection in the browser like localstorage for the next reload? And if it's so in the first mounting we have the default theme in the server like light and if in the browser it has a variable saying hey use the dark one so we see that light page for a second then when js has loaded on the browser the theme will have changed! (( a really bad problem ))

    • @hamedbahram
      @hamedbahram  Рік тому

      ThemeProvider automatically injects a script into next/head to update the html element with the correct attributes before the rest of your page loads. This means the page will not flash.

  • @aayush_24
    @aayush_24 Рік тому +1

    Hi Hamed,
    I've been following your videos . I am planning to work on an ecommerce project where there will be multiple frontends for client side, admin panel, probably mobile app and so on. previously i used to do such projects using rest api. but since next js is itself a fullstack framework and if i use it for client side, how am i supposed to design my entire project which includes other frontend too?

    • @hamedbahram
      @hamedbahram  Рік тому

      Hey Aayush! what do you mean by different frontends? do you mean different libraries, like react, vue, svelte, etc.? in that case a rest API would be the correct path. If by different frontends you mean, different designs, you can use styles to apply different themes, or if you want to build entirely different websites, you can use turborepo to share the core functionality while building different apps in the same repo. Does that answer your question?

    • @aayush_24
      @aayush_24 Рік тому

      @@hamedbahramby different front end
      I mean different react project. For example if my ecommerce client side is done using next framework, how can i use the framework's server routes for running admin panel(made using different react project )

    • @hamedbahram
      @hamedbahram  Рік тому

      For different projects (different react apps), you're best path is to build a REST API to all your different apps can talk to. That said, your admin panel, doesn't need to be an entirely different app, it can live in a route on the same project that's powering your ecommerce store.

  • @JIPPOKIKKA
    @JIPPOKIKKA Рік тому +1

    Do anyone have same problem in rendering. When I put Dark Mode and refresh then the light mode turn on and dark mode turn after that rendering. So this flashing. Do I need to save that value in Cookies?

    • @hamedbahram
      @hamedbahram  Рік тому

      I haven't run into this problem. But try passing the `suppressHydrationWarning` prop instead of setting the `class` and `style` attribute on the html tag. Let me know if that solves the problem.

    • @SingleSeeker
      @SingleSeeker Рік тому

      Have you solved the problem? I have the same situation.

  • @midoone188
    @midoone188 10 місяців тому +1

    Thanks hamed , but how to use next-themes with MUI ?

    • @hamedbahram
      @hamedbahram  10 місяців тому

      That'll make a good subject for another video.

    • @midoone188
      @midoone188 10 місяців тому

      @@hamedbahram thanks hamed .. waiting for this Video 😊

  • @sellimenes
    @sellimenes Рік тому +1

    Thank you for your effort and information. I have a question. I did implemented dark theme to my starter project as you do. But when I refresh the page, it always blinks the light mode, and then dark mode.
    Do you have ideas to solve this?

    • @hamedbahram
      @hamedbahram  Рік тому

      Try using the `suppressHydrationWarning` prop and remove the manual `class` and `style` attributes from the html tag.

    • @sohrabkhan101
      @sohrabkhan101 Рік тому +1

      @@hamedbahram i guess this will also leave the same issue.

    • @hamedbahram
      @hamedbahram  Рік тому +1

      @@sohrabkhan101 I'm not sure why that is, I don't seem to have that issue on my end.

    • @lasterius
      @lasterius Рік тому

      Did u solve ur problem? I have the same:(

    • @sellimenes
      @sellimenes Рік тому

      @@lasterius Well, technically, I didn't do anything different but I don't have this error anymore... Just update your nextjs version maybe?

  • @lazarechxartishvili
    @lazarechxartishvili Рік тому +1

    Thank You 😍

  • @mythsrin
    @mythsrin 7 місяців тому +1

    how to adjust theme for a particular section ??

    • @hamedbahram
      @hamedbahram  7 місяців тому

      set a class corresponding to the theme you want on the parent of that section.

  • @DavisonIncorp
    @DavisonIncorp Рік тому

    Is it still server side rendered then if we're wrapping the app in the Providers which is a use client component? Did we just lose server side rendering on the app? Sorry, still new to this.

    • @hamedbahram
      @hamedbahram  Рік тому +1

      No we haven't lost it ;) You can pass server components as `children` to client components and they'd still be server components - rendered on the server. However, if you _import_ a server component into a client component, it will become a client component. You can watch this video where I explain the new component composition in the `app` router => ua-cam.com/video/3Dw6D_WuzSE/v-deo.html

  • @AMIN_AI-lu1gn
    @AMIN_AI-lu1gn Рік тому +1

    its ok i can enable it and get this butto wor switch between dark&light but when i want to use dark: for dark them style it dont work

    • @hamedbahram
      @hamedbahram  Рік тому

      Clone my code and see what you're doing differently.

  • @즐거운코딩시간
    @즐거운코딩시간 Рік тому +1

    if i apply next-themes for dark mode, all of child component are client components?
    why i think like that because it is the Providers is 'use client'(client component) that surrounds {children}.
    please teaching me... thank you!

    • @hamedbahram
      @hamedbahram  Рік тому +1

      Good question! No, they won't become client components. If you _import_ a server component into a client component, it'll become a client component. However, if you pass server components as _children_ to client components, they will remain server components. You can watch this video to learn more => ua-cam.com/video/3Dw6D_WuzSE/v-deo.html

    • @즐거운코딩시간
      @즐거운코딩시간 Рік тому +1

      @@hamedbahram thank you for teaching!!!!! i love u :D

    • @hamedbahram
      @hamedbahram  Рік тому

      @@즐거운코딩시간 you're welcome!

  • @eliuddyn
    @eliuddyn Рік тому

    Amazing 🔥🔥

  • @27sosite73
    @27sosite73 10 місяців тому +1

    thank you

  • @ShokouhAskari
    @ShokouhAskari Рік тому

    thank you! it helps me a lot

    • @hamedbahram
      @hamedbahram  Рік тому

      You're welcome! Glad it was helpful.

  • @sahandsanaei3086
    @sahandsanaei3086 Рік тому

    great as always! thanks

  • @holyfreakinguacamole
    @holyfreakinguacamole Рік тому +1

    Everything renders and works appropriately, but wrapping the body tags with my provider gives two of these errors: Error: Hydration failed because the initial UI does not match what was rendered on the server. Warning: Expected server HTML to contain a matching in . and also.... one of these errors: Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering. My provider export also contains a context provider. Perhaps that is contributing to the error?

    • @hamedbahram
      @hamedbahram  Рік тому

      Not sure why you're getting these errors. Clone my repo and see if you still have the same problem.

    • @sanazniamati2025
      @sanazniamati2025 Рік тому

      I do it but I have 3 errors the same error @WisenTheMind . why?

    • @lindsayaiello501
      @lindsayaiello501 Рік тому

      I am getting the same errors as you. I am using the 'suppressHydrationWarning' attribute on html tag. Did you find a fix?

    • @TheAc3112
      @TheAc3112 9 місяців тому

      me too

  • @MudassirKhan-sx9jy
    @MudassirKhan-sx9jy Рік тому

    Hi, your content is amazing. i have a question if we wrap the whole app in client component then all the children components don't they become a client component.

    • @hamedbahram
      @hamedbahram  Рік тому

      Good question! No they won't become client components. Basically if you import a server component into a client component it'll become a client component. However if you pass server components as children to client components, they won't become a server component.

    • @MudassirKhan-sx9jy
      @MudassirKhan-sx9jy Рік тому +1

      @@hamedbahram thanks for response♥️. It was confusing for some time.

    • @hamedbahram
      @hamedbahram  Рік тому +1

      @@MudassirKhan-sx9jy anytime!

  • @naufalchoirulanandaa
    @naufalchoirulanandaa Рік тому +1

    To change the color when the dark mode is even darker, where is it?

    • @hamedbahram
      @hamedbahram  Рік тому

      I'm not sure if I understand the question ?!

  • @ErikaPfeiffer-rm7fk
    @ErikaPfeiffer-rm7fk Рік тому

    So we need next-themes just to toggle one CSS class?

    • @hamedbahram
      @hamedbahram  Рік тому +1

      It also reads the system preferences 😅

  • @udaym4204
    @udaym4204 Рік тому

    make video on backend also Thank you for spending your valuable time tech us 👍.

    • @hamedbahram
      @hamedbahram  Рік тому

      You're welcome Uday, thanks for your feedback. What type of backend videos would you like to see?

  • @demonak007
    @demonak007 Рік тому +1

    I learned something unexpected watching your video - I never realised double-clicking a file in VSCode opens it and removes the "preview mode" (italic name in the tab) status. I would always open the file, then ctrl+s to lock it in... *facepalm*

  • @Denn-t5t
    @Denn-t5t Рік тому

    Great materials and explanation, very useful 👍

  • @sahandsanaei555
    @sahandsanaei555 Рік тому

    How can i toggle favicon?

    • @hamedbahram
      @hamedbahram  Рік тому +1

      You can use the new metadata API to change the favicon programatically. You can read here: nextjs.org/docs/app/building-your-application/optimizing/metadata

  • @raphauy
    @raphauy Рік тому

    Ohh man, if you used typescript in your videos, they would be perfect

    • @hamedbahram
      @hamedbahram  Рік тому

      Thanks for your feedback Raphael.

    • @luqmanusman70
      @luqmanusman70 Рік тому +1

      @@hamedbahram Yes please, it is requested to please make all your future videos in typescript.

    • @hamedbahram
      @hamedbahram  Рік тому

      @@luqmanusman70 thanks for your feedback Luqman!

  • @gabrieldearruda5286
    @gabrieldearruda5286 Рік тому +1

    Wrapping the entire application with a "use client" component makes all children on the client side, right?

    • @hamedbahram
      @hamedbahram  Рік тому

      Nope it doesn't, you can pass server components as props to a client component without affecting the server/client boundary. I have a video on the channel about server and client components and when to use which, watch that to have a better understanding.

    • @gabrieldearruda5286
      @gabrieldearruda5286 Рік тому +1

      @@hamedbahram
      I placed a console.log('test') in a component that replaces {children} and a child of ThemeProvider, the console.log output appeared in the integrated VSCode terminal, so it was on the server side.
      I'm increasingly impressed with Next.js 13.

    • @hamedbahram
      @hamedbahram  Рік тому

      @@gabrieldearruda5286 cool!

  • @OnlyJavascript
    @OnlyJavascript Рік тому

    Am I the only one who experiences the same error when switching to dark mode and performing a manual page refresh?

    • @OnlyJavascript
      @OnlyJavascript Рік тому +1

      first option fails but supressHydrationWarning works fine in all the three modes.

    • @hamedbahram
      @hamedbahram  Рік тому

      What error are you experiencing?

    • @hamedbahram
      @hamedbahram  Рік тому

      That's right, setting the attributes manually would prevent reading the system preferences.

    • @OnlyJavascript
      @OnlyJavascript Рік тому

      @@hamedbahram @17:40 same error

    • @OnlyJavascript
      @OnlyJavascript Рік тому

      switch to dark theme and make a page refresh . the error still exists, sir.

  • @ehsankhorasani_
    @ehsankhorasani_ 8 місяців тому

    I though you're going to use it with server components

    • @hamedbahram
      @hamedbahram  8 місяців тому

      We are using the themes in our server components, however to share the global context or to switch the theme we need client components.

  • @es20x
    @es20x Рік тому

    Then the children components of a parent client component is server component 😅

    • @hamedbahram
      @hamedbahram  Рік тому

      That's the beauty! Interleaving client and server components as needed.

  • @dhirajavasthi6539
    @dhirajavasthi6539 Рік тому +1

    how can I pass context data to server component?

    • @hamedbahram
      @hamedbahram  Рік тому

      via the url.

    • @dhirajavasthi6539
      @dhirajavasthi6539 Рік тому

      Is there any other way to transmit data besides using a URL? It becomes quite confusing when we rely on context to handle states, yet we're unable to transmit the state to the server component. please help@@hamedbahram