Although I don’t reach for React Context all the time, there are some use cases where it can be a great tool. Recently, I had to use them in a TypeScript project with Hooks, and struggled for a bit.
I’ll demonstrate how to write a React Context in a clean and straightforward way with a Counter
component. This component will work like this:
From React documentation, we first need to use createContext
in order to define a context (with default values). As we are using TypeScript, we also need to define its shape.
#javascript #web-development #react #typescript