I’m trying to create an auth/session context for my application. However, I am facing some errors when I attempt to build the Provider component and pass the children as props.
const AuthContext = createContext({})
type Props = {
children: ReactNode
}
export const AuthProvider = ({children}: Props) => {
return(
<AuthContext.Provider>{children}</AuthContext>
)
}
The following errors are appearing:
Cannot find namespace ‘AuthContext’.ts(2503)
Unterminated regular expression literal.ts(1161)
I tried to write the code with more information to see if it was a type error, but it didn’t solve the problem. When I try to take the same code and change the extension from TSX to JS the errors disappear.
2
Answers
Change your file extension from
.ts
to.tsx
For the first issue that you are facing:- Cannot find namespace ‘AuthContext’.ts
Change your
.ts
file to.tsx
And in your
tsconfig.json
file make sure thatjsx
option is set toreact-jsx
undercompiler options
as below:-For your second issue:- unterminated expression
Just close your
<AuthContext.Provider>
with</AuthContext.Provider>
as below:-