Error: Element type is invalid

after including
import { AuthContext } from './shared/context/auth-context';
in app.js
I’m getting this error

Auth-context code

import {createContext} from 'react'

export const AuthContext = createContext({

We would need to see the content of the App.js component. It can be caused by a number of things. The error message gives you two possible suggestions.

Without more of the code, it’s hard to say anything. But it does seem a bit unlikely that the specific error is just from importing the context, surely you made other changes to the code. Like using the context in App for example?

when I dont import and comment the context code it works fine

App.js code

import './App.css';
import React,{useState,useCallback} from 'react';
import {
  BrowserRouter as Router,
} from 'react-router-dom';

import Navbar from './shared/components/navigation/Navbar';
import Event from './events/pages/Event'
import Details from './events/pages/Details';
import MakeEvents from './manage event/pages/ManageEvents';
import PlanEvent from './manage event/components/PlanEvent';
import Login from './shared/components/login/pages/Login';
import Auth from './user/pages/Auth';
import { AuthContext } from './shared/context/auth-context';

function App() {
  const [isLoggedIn,setLoggedIn] = useState(false);
  const login = useCallback(()=>{


  const logout = useCallback(()=>{


  return (
    <AuthContext.provider value={{isLoggedIn:isLoggedIn,login:login,logout:logout}}>


          <Route path='/' exact>

          <Route path="/:eventId/events" exact>

          <Route path="/test" exact>

          <Route path="/manageevent/" >
          <Route path="/auth" >
            {/* <Login/> */}

          <Redirect to="/"/>

export default App;

Maybe one of the components that are consuming the context is the issue. What happens if you comment out <Auth />.

This error is usually from an incorrect import/export or some invalid return value from a component.

If you have a GitHub repo you can link to that would help.

commented Auth but no change,
the repo is private :disappointed_relieved:

works well when I comment


What other components are consuming the context?

Check their (likely conditional) return is valid and double-check your import/export (e.g. no named exports for the components/pages).

At this point, everything is just speculation, which can go on forever. Without the code, we really have no way of knowing what is wrong.

Not sure if your code is based on this article, but it sure does look similar.

1 Like

I think it should be AuthContext.Provider (with captal P).


^ Good catch.

I’m actually a bit surprised no other error is provided in this case. Not even the linter is complaining as far as I can tell. I guess that’s what I get for not (rarely) using context.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.