Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find namespace 'ctx' error when creating Context with react - typescript

I'm working on a project with react using typescript and I'm having a bad time figuring out why this error is happening, basically, I can't use any createContext examples that I found on the internet because of this. This one specifically I got from here: https://github.com/typescript-cheatsheets/react-typescript-cheatsheet I'm trying to use the same that is showing in the Context section.

import * as React from "react";  export function createCtx<A>(defaultValue: A) { type UpdateType = React.Dispatch<React.SetStateAction<typeof defaultValue>>; const defaultUpdate: UpdateType = () => defaultValue; const ctx = React.createContext({     state: defaultValue,     update: defaultUpdate }); function Provider(props: React.PropsWithChildren<{}>) {     const [state, update] = React.useState(defaultValue);     return <ctx.Provider value={{ state, update }} {...props} />; } return [ctx, Provider] as [typeof ctx, typeof Provider]; } 

The issue is that everytime it says that there's this error Cannot find namespace ctx in the line:

        return <ctx.Provider value={{ state, update }} {...props} />; 

I'm still not able to figure out why, someone can see if I'm doing something wrong here? This is my tsconfig.json:

{ "compilerOptions": { "target": "es5", "lib": [   "dom",   "dom.iterable",   "esnext" ], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": false, "forceConsistentCasingInFileNames": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "preserve", "noImplicitAny": false, "strictNullChecks": false }, "include": [ "src" ] } 

Any help would be appreciated!

like image 220
Victor Carvalho Avatar asked Jul 28 '19 15:07

Victor Carvalho


People also ask

What is CTX in react?

ctx is a context object containing those properties (Source): pathname - path section of URL. query - query string section of URL parsed as an object. asPath - String of the actual path (including the query) shows in the browser.

How do you use JSX in TypeScript?

In order to use JSX you must do two things. TypeScript ships with three JSX modes: preserve , react , and react-native . These modes only affect the emit stage - type checking is unaffected. The preserve mode will keep the JSX as part of the output to be further consumed by another transform step (e.g. Babel).


2 Answers

Your file extension is most likely .ts instead of .tsx.

Therefore TypeScript is interpreting <ctx.Provider as cast and tries to find a type Provider in the namespace ctx.

like image 159
lukasgeiter Avatar answered Sep 30 '22 01:09

lukasgeiter


Please use tsx instead of ts it has some minute differences. tsx obviously allows the usage of jsx tags inside typescript.

like image 44
Sooraj Jose Avatar answered Sep 30 '22 01:09

Sooraj Jose