And when we use it, we need to create a state for it:
The dynamic looks something like this:
The problem with the stateless component is that we need to use the useState
hook every time, which might be annoying for components that require more properties and are all over our app.
So, it is ubiquitous to just put the state directly in the component. By doing this, we don’t need to have a state every time we use it, so then our Counter
component changes to something like this:
And then to use it as many times as we want without having to create a state for each:
The dynamic then looks like this:
Now, that’s great until we want to know the current state of the counter element from the parent element. So we might be tempted to create a monster like this one:
And then use it like this:
It might not be evident at first glance, but we are introducing side effects to every state change to keep the parent in sync with the children, and this has two significant issues:
One of the best things about hooks is when we create our own. The solution I’ll show next for this issue is quite simple, but I believe it solves most problems with state management I’ve seen around. The first step is similar to what we had at the beginning here; we create a stateless component:
But this time, instead of requiring the consumers of our component to figure out the state themselves, we create a hook that goes together with our component; we can call it useCounter
. The main requirement for this hook is that it needs to return an object with properties matching the properties of Counter
:
What this enables is that now we can use it almost as a stateful component:
But also, we can use it as a stateless component:
And we no longer have limitations accessing the state because the state is actually in the parent.
The dynamic then looks something like this:
With this approach, we are truly making our component reusable by not making it require a context or weird callbacks based on side effects or anything like that. Instead, we have a lovely pure stateless component with a hook that we can pass directly or partially if we want to take control of any property.
The name “paired hook” comes from providing a hook with a stateless component that can be “paired” to it.
The main issue the paired hook approach has is that now we need a hook for every component with some state. That is ok when we have a single component but becomes tricky when we have several components of the same type (for example, having a list of Counter
components).
We might be tempted to do something like this:
But the problem with this approach is that we’re going against the rules of hooks because we’re calling the useCounter
hook inside a loop. Now, if we think about it, we can loop over components that have a state of their own, so one viable solution is to create a “paired” version of our component, which calls the hook for us:
And then…
This approach seems similar to the stateful (the second example in this article) but is way more flexible and testable. The other method we have is to create a component context for every item without having to write a component ourselves, and for that, I made a small function that I published in npm called react-pair
:
The function is so simple that anyone could write it. The only difference is that I’m testing it, adding Developer Tools integration, and typing with TypeScript. I hosted the source on GitHub. The usage is quite simple, react-pair
provides a pair
function that the developer can use to create a component that gives access to the hook in a component context (without breaking the rules of hooks):
To be clear, react-pair
is not needed to achieve this. Anyone can manually create a new stateful component that pairs the hook with the component.
Using the util or not, the resulting dynamic looks something like this:
We get something similar to the stateful approach, but with less coupling and more flexibility; because the state doesn’t live inside the component, it lives “besides” it. So we have the cake and eat it too 🍰
I’ve been using this pattern for a while now, and so far, I haven’t found any blocking issues, so I invite the readers of this article to try it out in one of your projects and tell me how it goes!