无法从上下文中解构对象 属性

Cannot destructure property of object from context

由于新问题,重新发布与我上一个类似的问题。我正在尝试使用带挂钩的上下文来管理我的 React 应用程序中的身份验证。我收到错误 TypeError: Cannot destructure property 'isAuthenticated' of 'Object(...)(...)' as it is undefined.,但是当我 console.log 定义它的 属性 时,我看到 false 而不是 undefined

我在 authContext.js

中有上下文定义和提供程序
import React, { useState, useEffect, createContext } from "react";
import axios from "axios";

const AuthContext = createContext();
export { AuthContext };

const AuthContextProvider = (props) => {
  const [isAuthenticated, setIsAuthenticated] = useState(false);

  const setAuth = (boolean) => {
    setIsAuthenticated(boolean);
  };

  const apiOptions = {
    url: "users/is-verified",
    method: "GET",
    headers: {
      token: localStorage.token,
    },
  };

  function isAuth() {
    axios(apiOptions)
      .then((response) => {
        console.log("auth ran");
        const resData = response.data;
        resData === true ? setIsAuthenticated(true) : setIsAuthenticated(false);
      })
      .catch((error) => {
        console.log(error.response);
      });
  }

  useEffect(() => {
    isAuth();
  }, []);
  console.log(isAuthenticated);
  return (
    <AuthContext.Provider
      value={{ isAuthenticated, setIsAuthenticated, setAuth }}
    >
      {props.children}
    </AuthContext.Provider>
  );
};

export default AuthContextProvider;

然后我将我的路由封装在 app.js

中的提供程序中
import React from "react";
import {
  Switch,
  Route,
} from "react-router-dom";
import "./App.css";
import Register from "./components/pages/register";
import AuthContextProvider from "./components/context/authContext";
import RegisterRoutes from "./components/routing/registerRoutes";

function App() {

  return (
    <AuthContextProvider>
      <div className="App h-100 ">
        <Switch>
          <Route
            exact
            path="/register"
            render={(props) => (
              <RegisterRoutes {...props} />
            )}
          />
        </Switch>
      </div>
    </AuthContextProvider>
  );
}

export default App;

然后我有一个 RegisterRoutes 组件 returns 基于 isAuthenticated

的两个页面之一
import React, { useContext } from "react";
import AuthContext from "../context/authContext";
import { Redirect } from "react-router-dom";
import Register from "../pages/register";

function RegisterRoutes(props) {
  const { isAuthenticated, setAuth } = useContext(AuthContext);
  console.log(isAuthenticated);

  return !isAuthenticated ? (
    <Register {...props} setAuth={setAuth} />
  ) : (
    <Redirect to="/login" />
  );
}

export default RegisterRoutes;

您的默认导出是 AuthContextProvider(一个组件),而不是 AuthContext(一个上下文对象):这不会像您预期的那样工作。此外,您正在尝试将上下文对象导出到另一个对象中:

// WRONG: export context inside {}
const AuthContext = createContext();
export { AuthContext };

解决方案 1

改为正常导出上下文变量(不是默认情况):

// Export the variable
export const AuthContext = createContext();

// This works
import { AuthContext } from "../context/authContext";

方案二(推荐)

更好的做法是将上下文保存在单独的文件中并将其导出为默认文件:

// File AuthContext.js

import React, { createContext } from "react";

const AuthContext = createContext();

export default AuthContext;

// Import it in another file
import AuthContext from "./AuthContext.js";

我通过简单的操作解决了这个问题。在索引中,react-app,只是把我的供应商涉及到我的应用程序。

ReactDOM.render(
  <React.StrictMode>
    <AuthProvider>
      <App />
    </AuthProvider>
  </React.StrictMode>,
  document.getElementById("root")
);

我尝试在 localStorage 中设置日志记录时遇到同样的问题。

对于在页面组件中使用 gatsby 和上下文 API 的人:

您需要在 gatsby-browser.jsgatsby-ssr.js.

中使用上下文提供程序包装根元素

示例:

import React from 'react';

import { CustomProvider } from './src/context/CustomProvider';

export const wrapRootElement = ({ element }) => {
  return <CustomProvider>{element}</CustomProvider>;
};

试试你的 App.js 在 provider

中定义 value = {{state, dispatch}}
 <UserContext.Provider value ={{state, dispatch}}>
        <Routing/>
      </UserContext.Provider>

您必须将此 {} 放在其他文件中以在发送期间包装上下文

import {AuthContext} from "../context/authContext";

import { Redirect } from "react-router-dom";

import Register from "../pages/register";

function RegisterRoutes(props) {

  const { isAuthenticated, setAuth } = useContext(AuthContext);

这是我解决它的方法,用 authProvider.

包装 app.js 文件中的所有组件