Harnessing Client Components for Dynamic User Interactivity
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the Next.js Complex State Management Patterns with RSC course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Next.js Complex State Management Patterns with RSC, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:55] Welcome to lesson 2 of this module focused on client components. In this video I 'm going to show you what is the extent of the features or capabilities of client components. So client components are essentially designed and meant to harness the client-side environment fully. That means they have access to browser-specific APIs and many functionalities that are unavailable during server rendering time. First and foremost, client components have access to React's state full logic. That means that any hook like useState, useEffect and all the others that are related either to side effects or to state management can be accessed only through client components. This allows them to respond to user actions, update the UI on the fly and maintain a dynamic presentation. They help you provide immediate feedback to your users, improving the user experience essentially.
[00:56 - 03:33] This is one of the examples. Here this is a clear client component that uses local state to keep count of how many times the user has clicked on a button. That is something that we can do with client components. Another very clear example is the use of browser APIs such as the issue of location, the local storage if you want to keep track of information across multiple sessions and many more. In this example here we see how the Geo-locator component is capturing the location of the user and showing it on screen. This is something that can only be done because the component is a client component. Another key strength of client components is responding to user events. Being interactive, you can set up listeners to key events, keyboard inputs or any like really any browser event that you can think of and process the actions immediately without a round trip to the server and back. This example here is a very basic interactive data capture essentially from several form inputs on every keystroke of the user updating our state variable and keeping track of what the user is inputting. This is again something that can only be done on client side because we have access to the DOM API and all the events that come with it. Essentially and to close this very short overview of what client components can do, you should think of client components as the key to user interactivity, both in React and in Next as well. We're going to be focusing on Next but this can actually be applied to any React based framework. They can directly use an array of APIs, the browser capabilities and through it the devices capability. In many cases through the browsers you have access to other capabilities such as the audio API or like we saw the Geo Location API that are not specifically related to browsing or web apps but are definitely something to keep an eye on. Through them and all these capabilities you can create very rich user experiences. There are snappy and provide user feedback immediately which again will translate into a better user experience. Think of client components as a tool for you to enhance the user experience. Any kind of interactivity that you might need to show in your app would come through the client components. Now let's keep moving and go into the last video of this module before we move on into what server components are. So in the meantime, keep coding and I'll see you in