From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB
Unlock this course with a free trial
Join today to access over 25,200 courses taught by industry experts.
Global context for unread messages - Next.js Tutorial
From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB
Global context for unread messages
All right, guys. So we have it so we can mark our messages as read or mark as new. And it works, right? If I click mark as read, it changes in the database. It shows us a success. It even changes the button. But this doesn't change right here, this notification, until I reload the page. So what I want to happen is I want to click the button and have that change right away. And in order to do that, since these are two completely different components, we're going to be using global state using the context API that React provides. All right, so we're going to change some things around. Basically, the unread message count, which is this right here, the number, instead of having this in local state, in component state, it's going to be in a global context. So let's do that. Let's create in the root. We're going to have a new folder called context. And if you had a lot of global state, a lot of context, you could have separate files. Like we could have a message context for this. But since…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
(Locked)
Message model and form state12m 59s
-
(Locked)
Message submit and API route14m 22s
-
(Locked)
Require auth for contact form3m 53s
-
(Locked)
Fetch messages10m 30s
-
(Locked)
Display messages11m 15s
-
(Locked)
Mark as read15m 19s
-
(Locked)
Delete messages8m 38s
-
(Locked)
Order new first3m 25s
-
(Locked)
New message count display11m 5s
-
(Locked)
Global context for unread messages11m 6s
-
(Locked)
-