Keep your state setters close
- Context
- React
- Pattern
- Colocation

I'm sure you've seen something like this before:
This is an example of poor data encapsulation. Here we can see parent context state setters are directly exposed to children.
Why is it wrong?
It means that the code which updates the state will be scattered all over the place, thus hard to follow and debug. This will make us suffer in the long run 🤕.
The solution
Create handlers for your parent component state related updates and make them available under a dedicated property. DO NOT expose state setters directly.
The code
Let's create context according to Kent C. Dodd's approach. Notice that i have deliberately omitted state setters in context type declaration. The handlers are created in the body of InputContextProvider; thus are close to related state setters.
Now let's revisit the first example
We can use the handlers in our consumer component.
The Conclusion
Our code is easier to follow and maintain.
I'm aware that the first example is a bit of an exaggeration, though i found it appropriate to highlight what problem we are trying to solve.
Thanks for your time, and see you next time.