Darius Cosden πŸ§‘πŸ»β€πŸ’»'s Avatar

Darius Cosden πŸ§‘πŸ»β€πŸ’»

@cosden.dev.bsky.social

Teaching React at @cosdensolutions Β· 150k subs Β· Building an AI learning platform

71 Followers  |  52 Following  |  232 Posts  |  Joined: 01.12.2024  |  2.6012

Latest posts by cosden.dev on Bluesky

Post image

With this, the server components stay rendered on the server, and the client component holds all the interactivity

This gets you the best of both worlds

Really cool pattern huh? Try it out!

12.08.2025 15:00 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0
Post image

The solution is to create a ClientComponent that has that can toggle between both components with state

This way, the client decides what should render, and the framework fetches the correct component based on the state variable

And yes, this works with server components

12.08.2025 15:00 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

Here's our situation:

These are all server components and they ideally should remain on the server

But we don't want to show both at the same time. We want to be able to toggle between them

That can only be done on the client

How can we do it?

12.08.2025 15:00 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0

Here's a cool pattern to use in React if you're combining server and client components

The goal is to separate the client rich interactivity from the efficiency of running your code on the server as much as possible

The result is truly the best of both worlds

12.08.2025 15:00 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

This has many benefits:

1. It doesn't use useEffect
2. It has no side effects
3. No dependency arrays
3. It doesn't cause a re-render due to using ref
4. It's super simple

Bonus: Make it into a custom hook and reuse it

08.08.2025 10:02 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0
Post image

First, understand that useEffect isn't the right tool for this

useEffect is meant for running code in side effects, not firing things once

Instead of using a useEffect, you should run code directly during render with a ref

Update the ref when done and it won't run again

08.08.2025 10:02 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

Sometimes in React, you want to run code in a useEffect only once

Even if the code dependencies change

Unfortunately, React doesn't like this (even though it's a valid use case)

But don't worry, I got you. Here's how you can do it

08.08.2025 10:02 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
How to Run Code When Window Size Changes in React
Run code when window size changes in React #react #nextjs #frontend #code #reactjs How to Run Code When Window Size Changes in React

Just posted a video about this as well

youtube.com/shorts/dAKm...

06.08.2025 09:59 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0
Post image

And that's it, that's the entire functionality

You can wrap it in a custom hook useWindowSize that returns the size

This can now be used anywhere in your application where you need this functionality!

06.08.2025 09:59 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

Then, using a useEffect, hook into the window's "resize" event

Create an empty effect with no dependencies, and define your handleResize function

Then add a listener using window.addEventListener on the "resize" event

Don't forget to clean up the listener on unmount!

06.08.2025 09:59 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

First, you need a state variable to hold the width and height of the window

This will enable your React app to react (no pun intended) to changes to it

06.08.2025 09:59 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

In React, sometimes you need to run some code when the browser window size changes

Here's how to do it cleanly by hooking into the window's "resize" event

Bonus: you can create a really nice custom hook out of it

06.08.2025 09:59 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Preview
Cosden Code Waitlist Waitlist

Join the waitlist cosdencode.ai

05.08.2025 10:02 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0

It's also way simpler to implement. I don't need to host my own infra or spend time writing tests

I can just focus on creating the courses

And if your system prompt is good, this works really well

05.08.2025 10:02 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image Post image

Instead of running automated tests, or even having a human review lesson submissions, the AI does it

The feedback is much more personal and thoughtful, and actionable

This fits with the overall platform being human-created courses delivered by AI

05.08.2025 10:02 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

AI is grading my students on my dev learning platform

And it's much better than a human or automated tests

This unlocks a whole new way of personalized learning

05.08.2025 10:02 β€” πŸ‘ 1    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
How to Update State Objects in React
How to handle state objects in React #react #nextjs #frontend #code #reactjs How to Update State Objects in React

Just made a video on this as well

youtube.com/shorts/zum_...

04.08.2025 15:04 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0

With this, your state updates are much simpler!

Fun fact, this is actually what Redux Toolkit uses by default for their reducers

So it's worth a shot

04.08.2025 15:04 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

By using the "produce" function, you can write your updates by applying them to a draft instead of the actual state object

This looks like mutating code (which you can't do in React) but it's actually not

The produce function will do the spreading for you automatically

04.08.2025 15:04 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Introduction to Immer | Immer Immer (German for: always) is a tiny package that allows you to work with immutable state in a more convenient way.

You can use the immer library to make your updates

It allows you to write much simpler code

immerjs.github.io/immer/

04.08.2025 15:04 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

Updating a property on a deeply nested state object in React can be cumbersome

You have to spread every layer until you get to the one you need to change

This isn't fun, especially if you are doing it often

Here's a much simpler way to do it

04.08.2025 15:04 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Authentication Gone Wrong (React Code Review)
πŸ“₯ SECRET PROJECT (Newsletter) β†’ https://cosdencode.aiπŸ“ˆ Advanced Patterns React β†’ https://cosden.solutions/advanced-patterns-reactπŸš€ Project React β†’ https... Authentication Gone Wrong (React Code Review)

Here's the full video

youtu.be/ZIhgAZ8ZH9c

01.08.2025 10:01 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0
Post image

Horrible UX and DX. Don't do this

Here's the solution:

Since we have server components, just do the auth check there on the server

No need for client components, and the redirect can happen before anything is rendered

No useEffects, no ProtectedRoute needed

Much simpler code

01.08.2025 10:01 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0

Because this is a client component, this auth check happens after all server components, after the JS bundle, and finally the component mounted

This is super inefficient

Even worse, the posts are fetched regardless of auth. They are just hidden from the UI until auth is fetched

01.08.2025 10:01 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

Well, if we look at ProtectedRoute, we can immediately see the problem (removed all non relevant code)

It is a client component, and on mount it will fetch and check user token

If there is no token, it will redirect user to login page

Can you see the problem?

01.08.2025 10:01 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Post image

Consider this component, seems pretty innocent, right?

It's a server component, fetches the posts, and passes it to PostsPage (client component)

The auth is handled in ProtectedRoute

01.08.2025 10:01 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0

So many React devs implement authentication wrong in their apps

I just reviewed this code on the channel and there are some lessons here for every React dev

Here is how not to do authentication in React

01.08.2025 10:01 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Preview
Cosden Code Waitlist Waitlist

Join the waitlist cosdencode.ai

31.07.2025 10:00 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0
Post image

One of the biggest advantages of my AI learning platform is that I don't have to spend so much time creating entire courses

An outline with details and good prompts is more than enough

The AI does the rest

The result is actually better than if a human were to teach

31.07.2025 10:00 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 1    πŸ“Œ 0
Why Is Everyone Hating This React Trick?
You Don't Know How This React Library Works #react #nextjs #frontend #code #reactjs Why Is Everyone Hating This React Trick?

Just posted a video version of this

youtube.com/shorts/P3dF...

30.07.2025 10:02 β€” πŸ‘ 0    πŸ” 0    πŸ’¬ 0    πŸ“Œ 0

@cosden.dev is following 20 prominent accounts