I am having a pure Typescript class that does a homogenous activity as a utility class. I have created a context that can be used anywhere, and I wanted to know if we could use this context and its values in a pure Typescript class.
The class looks like this:
export class UtilityClass {
public async method1() {
...
}
public async method2() {
...
}
}
I would like to use the Context like this:
const { total, cartItems } = useContext(CartContext);
I have tried by creating a static context type like this in the class:
export class UtilityClass {
static contextType = CartContext
public async method1() {
const { total, cartItems } = useContext(CartContext);
...
}
public async method2() {
...
}
}
But this is throwing following error:
any
Property 'context' does not exist on type 'UtilityClass'.