Let’s end the discussion! "use client" vs "use server" Client or Server Component | Next.js

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

КОМЕНТАРІ • 20

  • @ibrahim_youssef_13.12
    @ibrahim_youssef_13.12 2 місяці тому +1

    Believe it or not, this very smart and very handsome young man has solved many problems and explained things that amounted to months of solving errors. Please continue and explain the use server and fetching data in a separate video, and follow all the problems in the next section in particular every code related to Next please.

  • @MarceloGuaycurus-o1k
    @MarceloGuaycurus-o1k Місяць тому +1

    Fantastic video. Claude 3.5 should watch this. It uses client components everywhere

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

    Sorry for the big message in advance :c.
    IMPORTANT: Both Server and Client components are pre-rendered on the server at built time for the initial page load, that's why Client component loggs appear on the server console.
    Server components only run once, they do not re-render. Yes they can "re-render" if you request them again from the server but in that case it's more like they're "re-building" instead of "re-rendering".
    After the initial page load, Client components are only re-rendered on the client. What "use client" directive does is telling the Next.js bundler "send this chunk of code and everything that is owned by this file to the browser", which means if you import a component -- either a Server o Client component -- this code and everything under it will be sent to the client as well. This means you're essentially defining a boundary, and everything inside that boundary will run on the client.
    Like he said, Next.js treats all components as Server components by default, which means you need to opt out of that in case you need to use client-only features like React State, LifeCycle methods (mount, unmount, etc.), the window object and client-only APIs. Only in these situations you should be tempted to write "use client" at the top of the file, and you should try to do it only on very specific components, not the whole page.
    On the other hand "use server" is not the opposite of "use client", 11:40 NO, you CAN'T and you SHOULDN'T USE this directive on the top of a (default) server component, and certainly not on the top of a file that will be imported inside a "use client" boundary in order to make it run on the server again. "use server" directive tells the bundler "turn all exported async functions into endpoints, expose them for the client to use".
    Basically, "use client" means -> send this chunk of code to the client, either to hydrate or run client side APIs, and run on the client from now on.
    "use server" means -> "expose these async functions as POST endpoints to be hit". Only async functions are allowed in a file with "use server" directive at the top.
    WHY CAN'T SERVER COMPONENTS BE IMPORTED INTO CLIENT COMPONENTS AND REMAIN SERVER ONLY?
    When Next.js builds the app, it will run and render all server components first and then run and render the client components, if it encounters a client bouldary ("use client") it will execute that code once all the server components were rendered and turned into RSC Payload.

    • @tobitacklestech
      @tobitacklestech  Місяць тому +1

      Wow 😯, nothing to add here, thank you

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

      @@tobitacklestech No no, happy to help! Let me know if I can help with anything else :D

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

      @@Luisllaboj19thanks a lot man! 🎉

  • @meetsuthar4118
    @meetsuthar4118 2 місяці тому

    I liked the way u explain hard topic is much simpler way. Keep it up bro !

  • @ajayrathod7438
    @ajayrathod7438 2 місяці тому

    That was very helpful thanks

  • @ayushshende4290
    @ayushshende4290 2 місяці тому

    Client would be very useful when you want to work with a custom backend like express or django

  • @Lucifeier
    @Lucifeier 2 місяці тому

    Thank you for the video, btw what is the whiteboard app you are using now? That seem nice!

    • @tobitacklestech
      @tobitacklestech  2 місяці тому +1

      @@Lucifeier this was excalidraw, it is on the Web 🔥

  • @codeinspector
    @codeinspector 2 місяці тому

    Thanks :)

  • @MetalPotionEhUmCu
    @MetalPotionEhUmCu 2 місяці тому

    who was debating about this?

    • @tobitacklestech
      @tobitacklestech  2 місяці тому

      There are always discussions about that, especially if you learn Next js 👀

  • @anhvuuc8693
    @anhvuuc8693 2 місяці тому

    verceo made nextjs harder 😂 they was the best of the best but now …

    • @tobitacklestech
      @tobitacklestech  2 місяці тому

      I think they still do a very Great Job, New Technologies just need new solutions 👀