Next.js 13 Context API Tutorial

Поделиться
HTML-код
  • Опубликовано: 31 дек 2024

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

  • @NativeNotify
    @NativeNotify  Год назад +3

    For those who use Redux Toolkit, I just made a Next.js 13 Redux Toolkit video. You can find it here: ruclips.net/video/Onekwb2l9zg/видео.html

  • @aarongarcia2433
    @aarongarcia2433 Год назад +9

    Just what I needed, thank you! I was bummed when the next/beta docs were lacking under the context section, but this fulfilled it perfectly! I also love how you got right into the material. Excellent video!

  • @johnnydoe2250
    @johnnydoe2250 Год назад +15

    In this case all your application will work client side, a server component inside a client component will also become client and since root layout page is loaded on every page all your components will be client side, you lose therefore the main interest of next, and moreover it can pose some problems of scalability, because you no longer have access to the next's functions which run on the server side

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

      So does this mean that when you check your app in chrome using the dev tools, the preview network for server side rendering result will show nothing? (Just like react)?

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

      Thanks man. I was supspecting the same thing too.

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

      My thoughts exactly. Thanks for confirming.

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

      how can you solve this problem so ?

  • @koozkotopia
    @koozkotopia Год назад +1

    Thank you so much for this video, I spent a whole day to figure out how to wrap the AuthProvider around the children in Layout.tsx and ran into all kinds of errors. This video is the only one I found that shows what to do when there is no

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

    My friend! I was just looking for a good way of using context with next 13! Thanks my friend

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

      Yeah I realized after making my crash course on Next.js 13, server side components don't work with Context API. I'm thankful you can still use Context, it's just a more complicated setup

  • @sumitsaha6941
    @sumitsaha6941 9 месяцев назад +1

    This is the best video for context api in nextjs.

  • @merakli2022
    @merakli2022 Год назад +8

    what is the advantage of nextjs over reactjs if we have to use "use client" directive to convert a server component into a client one? What am I missing here? Thanks

    • @HarryRonchetti
      @HarryRonchetti Год назад +1

      @merakli2022 I was thinking this too, I've just trialed this method and I think using 'use client' only in the context store/component rather than at a page level actually still means the rest of the page is still SSR.
      Inspecting the page source after the build I can still see the rest of the page content is still there so Next.js would still be beneficial as we're not outputting a SPA but a mixture of SSR and client only content (if that makes sense)

  • @MudassirKhan-sx9jy
    @MudassirKhan-sx9jy Год назад +8

    if every other component is render under a client component GlobalThemeProvide then what is purpose of using nextjs?

    • @alerodriguezn
      @alerodriguezn Год назад +7

      Exactly, it doesn't make sense to use context, the idea is to avoid client components. Obviously you have to use client components, but the idea is to declare them in the child components

    • @vincentm3135
      @vincentm3135 Год назад +1

      @@alerodriguezn I stopped the video halfway through for the same reason. What I love about nextjs is how easy it is to write full-stack code, without jumping into hook hell. I'm trying to get away with that and 13.1 delivers; I'm not going to sabotage sever components by wrapping them in a hacked client context provider.

  • @sahasrabhujaneuron5975
    @sahasrabhujaneuron5975 11 месяцев назад +1

    Awesome! This is what I'm looking for. Many thanks!

  • @SheisaCreative
    @SheisaCreative Год назад +1

    Thank you! Exactly what I was looking for. 😊

  • @techug2719
    @techug2719 7 месяцев назад

    Im on next 14....this is still useful🤝 thank you kind sir!

  • @rajatmasih3706
    @rajatmasih3706 Год назад +1

    Amazing tutorial bro i was trying to learning Context api from many days but not able to understand.
    After watching your video Now i understand how it works.
    Thanks man!😄

  • @TheWabbbit
    @TheWabbbit Год назад +1

    Does that mean, to use context globally in next.js app, the whole app has to be an client component?

    • @NativeNotify
      @NativeNotify  Год назад +1

      No, you still have to declare if a component is a client component. This will not make every component a client side component

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

    Thank you so much!
    How did you end up with this logic? Could not find this in the doc

  • @Juan.amaral_
    @Juan.amaral_ Год назад

    I can't build the project with the error in "children".... how do I build it?

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

    You are life saver. Thank you so much for sharing this.

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

    there is an issue with context, if you need to support few languages and when you switching locales (using middleware) for example: from /en/home to /de/home, all context will be lost

  •  Год назад

    It works but does not it go against all the server-side Next JS' purpose?

  • @omardavila8165
    @omardavila8165 10 месяцев назад

    Is it going to be the same in Next.js 14?

  • @heyneisha3816
    @heyneisha3816 Год назад +2

    Thanks for creating this video! However I am having trouble adding the Global Context Provider to layout.js. I am getting the following error: error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (). I am not having much luck online, and was wondering if you have come across this error. The provider is already written in an arrow function which is the suggested workaround, but that doesn't appear to be working.

    • @kyoukisu3628
      @kyoukisu3628 Год назад +1

      i got same error

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

      @@kyoukisu3628 thanks for your help! I will try this out!

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

      got the same problem, what di you do to fix it?

    • @heyneisha3816
      @heyneisha3816 Год назад +1

      @johannranudd6671 I'm not sure how it happened but when I responded to @kyoukisu3628 I overwrote his reply. Unfortunately I can't remember the github link that was added. After some more digging I finally came across another github ticket mentioning my exact problem. The suggested fix was to upgrade my current version of 13.3.0 to 13.3.1-canary.4(npm i next@13.3.1-canary.4) which fixed the jSON.parse issue. I then ran into a next auth dependency conflict which I used npm i next-auth --legacy-peer-deps to get pass that error. Although I was not happy with that solution I am still able to persist and retrieve data from my database. Hope this helps.

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

      @@danielszepesi1558 Thank you!

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

    One question. It's OK turn "use client" the root "page.tsx" or not problem? Thanks to share this buddy.

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

    why will I need useContext when you are just using setState in another file. Set the values in GlobalContextProvider file and use it in another file. See if it works

  • @eliuddyn
    @eliuddyn Год назад +1

    Amazing 🔥🔥

  • @katiegerg7071
    @katiegerg7071 Год назад +1

    Amazing, thank you!

  • @madhavdutta6018
    @madhavdutta6018 Год назад +4

    You can fix the children error by just passing the type like this
    export const GlobalContextProvider = ({ children }:{children:ReactNode}) => {

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

      Thanks so much for pointing that out!

  • @jvlob
    @jvlob Год назад +2

    Isn't there a way of passing on the `value` prop all from GlobalContext without having to specify every single one of the values? You can end up having a lot of properties and setters, and if you always have to pass them inside the value prop, it can get huge and ugly very quickly.

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

    this context not work in mobile devices?

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

    What if you have server components in which you fetch data, and you need user information from the context to make the API call? This must be a common problem.. how do I solve it?

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

      You can only get the data from a client side component. So an option would be to create a client side API component and import that into your server side component

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

      @@NativeNotify ok but doesn’t that then remove the benefit from having a server side component that fetches the data? That data then has to be fetched from the client component inside the server component?

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

    what if i build the project, is the build will failed because the error?

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

      You could try to put : any after the children object to make the error go away

  • @frankylopez4477
    @frankylopez4477 Год назад +1

    Thanks!

  • @juliscapucin
    @juliscapucin Год назад +1

    Thanks a lot!

  • @WilsonCollantesAponte
    @WilsonCollantesAponte 11 месяцев назад +1

    Crack

  • @sofiiavozniak4100
    @sofiiavozniak4100 10 месяцев назад

    THANK YOU!!!!

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

    I have this error that says "TypeError: Cannot read properties of null (reading 'useContext')". by the way this is a normal js file. not using typescript. could anyone enlighten me? TIA!
    "use client";
    import { createContext, useContext, useState } from "react";
    const GlobalContext = createContext();
    export default function GlobalContextProvider({ children }) {
    const [loggedIn, setLoggedIn] = useState(false);
    return (

    {children}

    );
    };
    export const useGlobalContext = useContext(GlobalContext);

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

      update: already solved this.
      export const GlobalContext = createContext({});
      ...and...
      export function useGlobalContext() {
      return useContext(GlobalContext);
      }

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

    Thank you!

  • @reactjs1900
    @reactjs1900 Год назад +1

    Only use client

  • @muhsincan.yilmaz
    @muhsincan.yilmaz 7 месяцев назад

    Thank you

  • @tarungupta5247
    @tarungupta5247 10 месяцев назад

    watching on 14th feb 2024

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

    Hey, nice tutorial 👏👏
    Also, Jack showed a way with zustang where state can also be access in server components.
    ruclips.net/video/OpMAH2hzKi8/видео.html
    Is there a way to do so with Context API?

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

    Please, do not assign the entire page as a client side component! 😢

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

    I disliked this because a tutorial is not "WHAT" you're doing, rather it is "WHY" you're doing it?

  • @시
    @시 Год назад +1

    You saved my life

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

    You saved me hours man thank you for a simple quick answer.
    const [newSubscriber, setNewSubscriber] = useState(true); lol

  • @oguzdurmaz4893
    @oguzdurmaz4893 Год назад +1

    thank you!