The State of The State In The App Router

Rate this content
Bookmark
The talk focuses on state management in React applications, especially within the context of the AppRouter. The importance of avoiding global state is emphasized, and the concept of using a store provider component to dynamically create a store for each request is discussed. Zustand is highlighted as a favored state manager for its ease of use and ability to create multiple stores co-located with components. The talk also covers the significance of separating mutable and immutable data, and the benefits of using React's built-in hooks like useState and useReducer. The AppRouter in Next.js is praised for its ability to manage route state and searchParamsState, making it easier to handle state without a state manager. The use of React context for low-velocity data and the advantages of SSR capabilities in Next.js are also discussed. Finally, practical advice is given on managing state in modern React applications, including the use of React Query and SWR for data fetching.

From Author:

NextJS 13.4 changed the game with the App Router and React Server components. The change to the state management model looks simple to start, but gets complex very quickly as you try to create the practical every-day user experiences that were straightforward in the pages model. We'll talk about the different state management models, how they work in the App Router, and how to decide which you should use.

This talk has been presented at React Advanced Conference 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Jack Harrington is a FullStack engineer and YouTuber who runs the YouTube channel called the Blue Collar Coder.

Jack Harrington covers a variety of topics on his YouTube channel, including React, state management, and the AppRouter.

Jack Harrington mentions several state management tools such as Redux, MobX, Zustand, and Jotai.

The main point of Jack Harrington's talk is to share pragmatic rules on how to use a state manager properly and safely in React and Next.js applications.

Jack Harrington suggests that you might not need a state manager due to the advanced state management capabilities built into React, such as useState, useReducer, and useEffect.

The first rule Jack Harrington mentions is 'no global state,' which means avoiding global variables to prevent issues like cross-request data pollution.

According to Jack Harrington, the AppRouter in Next.js is significant because it can manage route state, parameterized routes, and searchParamsState, making it easier to handle state without a state manager.

For managing low-velocity data, Jack Harrington recommends using basic off-the-shelf React context.

Jack Harrington favors Zustand because it is easy to use and allows for creating multiple stores, which can be co-located with the components that need them.

The key benefit of using multiple stores in Zustand is to avoid the complexity and issues associated with global state, such as cross-request data pollution.

Jack Herrington
Jack Herrington
32 min
20 Oct, 2023

Comments

Sign in or register to post your comment.

Video Transcription

1. Introduction to State Manager

Short description:

I'm Jack Harrington, a FullStack engineer and YouTuber. I cover React, state management, and the AppRouter on my YouTube channel called the Blue Collar Coder. I learned from experience and feedback and will share pragmatic rules for using a state manager properly and safely.

All right. Yeah, I'm Jack Harrington. I am not just a FullStack engineer like a lot of you, I'm sure. I'm also a YouTuber. So I have this YouTube channel called the Blue Collar Coder, and on there I cover a bunch of different topics. So, a lot of React, obviously. And I talk a lot about state management. So I cover in-depth details on that, and I talk a lot about the AppRouter, because it's next to the S and it's very popular, and it's the new router, and I get a lot of feedback because I cover a lot of these things. Like, hey, how do I use my Redux store in the AppRouter? And I'm like, that's an interesting question. So I did a bunch of coding to see myself, how I would do that. And I knew at the time, I was like, hmm, this seems to work. It definitely does work, but maybe this is not the right way. So I put out a video. Boom. And it actually did pretty good. People seemed to like it on the comment side. In fact, when I took it down, because there are some anti-patterns in it, they were like, where'd it go? But I got some feedback from the React team and from the Next.js team, like, you know, there's some things you shouldn't do in there. So what I'm here to do today is to give you all of the experience that I learned from that. I went back and did a lot more coding, got a lot more feedback. And what I'm hoping to present today to you are some basic pragmatic rules around how to use a state manager properly and safely. And maybe if you should or shouldn't in the App Writer.

2. React State Management and Redux Safety Tips

Short description:

React has improved state management with built-in hooks like useState, useReducer, and useRef. Next.js also provides state management options like route state and searchParamsState. This means that you might not need a separate state manager. However, if you still want to use Redux, here's a safety tip: avoid global state.

And interesting, let's go back to beginning on this. So common thought around React has been... I mean, it's only been around for ten years. But for most of that, or a lot of that, we've had the class components, and this.state and this.setState. And that wasn't great, from a state management point of view. So that's why you run into things like Redux, MobX, to make it a better experience.

But now, you have to think differently about it. Because we've got better state management built right into React. You've got useState, useReducer, useRef to store a state. You've got useCallback, useMemo, useEffect to listen for changes in the states. And that is basically a reactive state management system built right into the core of React. So maybe we don't need a state manager.

And then on top of that, we've got Next.js, and the operator has the route state. You've got the parameterized routes, you've got the params, you've got searchParamsState. You looked at Lee Robinson, he did a fantastic video recently on how to use searchParamsState to hold state and manage state in a Next.js application. You have form state, you've got server actions, you've got revalidation, you can do things like HTMX and have server-side driven development. So end result is rule number 0, you might not need a state manager. So think about it that way first. When you think about your new project or migrating, you know, maybe a state manager is not gonna be a part of that.

It's weird. You come with a talk on state management, and I'm the guy that's telling you maybe you don't need a state manager. But maybe you're like, oh, I'm really sad. I love my state manager. I love how it works. I don't want to do dependency arrays. So OK. Let me give you some safety tips for how to do it safely if you want to use a Redux store still. So rule number 1, no global state. So what do I mean by that? Well, OK, let's think about a global. You got your configuration store here.

QnA