Because really this is just a wrapper around this render row function. I've used this pattern before in production is freaking amazing.
So another thing that can like catch you out is React's built-in types. Now, these are actually maintained separately from React itself. React isn't written in TypeScript. It's written in Flow I think. And what it does then is like you have to install React and then install types React. And the kind of like, because they're not built together, they can often kind of like, the types of doing something slightly different to the actual React core, not in a dangerous way, but there's just some stuff that's in the types that React itself hasn't documented. And this can be really, really painful.
So let's go with like a simple one here, which is let's say you have a number in our state. Now, as predicted, if you start off with like a number in your USE state here, then this state is gonna be typed as number and set state is gonna be typed as number two. So I can't pass in a string into this, I'm gonna have to pass in a number. And these are all sort of built-in parameters in React, of course, if you use them.
Now, what happens then if I don't actually have a value to pass in here, but I still want this state to be typed as a number? Well, you can pass this a type argument. And so you can say, okay, this you state is now a number. And you might think that state now is typed as number because I can set state to number, but it's not, it's actually typed as number or undefined. And that's correct because the first render here, before I set my state, imagine if I do this in like a, I know this is a bad practice nowadays, not very fashionable, but let's say I just set my state to one inside this use effect. Well, on the first render state is actually going to be undefined because I'm passing nothing to this. And whereas if I actually pass it a number, then it's going to be defined on that first render. Now, the way it can do this, the way it knows how to do this is if we command click into you state here, we end up in this very, very terrifying file. And of course, I've immediately lost my place where we basically inside the type definitions for all of these hooks. So we've got use context, we've got use state, you know, oh, hello guys, all the gangs all here. And what we've got then, is we have a function overload on useState. You state can be called in two different ways depending on what you want, basically. So this first overload here, this is really hard to read. So let me sort of break it up a little bit. What's going on is that useState, it's a generic function, we've seen a generic before. And it's saying the initial state is this S or a function that returns S. Don't save my changes, please. I don't wanna be responsible for breaking anything.
Comments