1

嗨,我仍然是新的反应,我有一个问题和错误:

错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入

.

我创建了 auth 组件,错误提到“检查渲染方法AuthProvider”我怀疑错误来自 auth 组件中的“AuthProvider”函数,我不知道如何解决它。

屏幕截图: https ://imgur.com/CC7uO6A

auth.js

import React, { useReducer, createContext } from "react";


const AuthContext = createContext({
  user: null,
  login: (userData) => {},
  logout: () => {},
});

function authReducer(state, action) {
  switch (action.type) {
    case "LOGIN":
      return {
        ...state,
        user: action.payload,
      };
    case "LOGOUT":
      return {
        ...state,
        user: null,
      };
    default:
      return state;
  }
}

function AuthProvider(props) {
  const [state, dispatch] = useReducer(authReducer, { user: null });

  function login(userData) {
    dispatch({
      type: "LOGIN",
      payload: userData,
    });
  }

  function logout() {
    dispatch({ type: "LOGOUT" });
  }

  return (
    <AuthContext.provider
      value={{ user: state.user, login, logout }}
      {...props}
    />
  );
}

export { AuthContext, AuthProvider };

应用程序.js

//import from react library
import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";

//import from component folder
import { AuthProvider } from "./config/auth";
import Dashboard from "./dashboard/pages/Dashboard";
import Signin from "./login/pages/Signin";
import AdminPage from "./admin/pages/AdminPage";
import Login from "./login/pages/Login";

// import AssetPage from './asset/pages/AssetPage'
// import MaterialTables from './component/MaterialTable'

function App() {
  return (
    <>
      <AuthProvider>
        <Router>
          <Route exact path="/" component={Login} />
          <Route path="/dashboard" component={Dashboard} />
          <Route path="/admin" component={AdminPage} />
          {/* <Route path="/login" component={Login} /> */}

          {/* <Route path="/asset" component={AssetPage}/> */}
        </Router>
      </AuthProvider>
    </>
  );
}

export default App;
4

0 回答 0