React – 登出 UseContext

huangapple go评论126阅读模式
英文:

React - Log Out UseContext

问题

以下是您要翻译的部分:

// CONTEXTS
import User from "./contexts/userContext";

export default function App() {
  const [user, setUser] = useState(null);

  return (
    <main id="App" className="App">
      <ToastContainer position="top-left" autoClose={5000} />
      <User.Provider value={{ user, setUser }}>
        <Router>
          {user ? <Header /> : <HeaderAuth />}
          <Routes>
            <Route exact path="/" element={<Login />} />
            <Route exact path="/register" element={<Register />} />
            <Route
              exact
              path="/login/identity/forgotten-password"
              element={<Password />}
            />
            <Route exact path="/hp" element={<HomePage />} />
            <Route exact path="/profile" element={<Profile />} />
          </Routes>
          <Footer />
        </Router>
      </User.Provider>
    </main>
  );
}
import { NavLink } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import { useContext } from "react";

// CONTEXT
import User from "../../../contexts/userContext";

export default function Menu() {
  const [currentUser, setCurrentUser] = useContext(User);
  const navigate = useNavigate();

  const handleLogOut = () => {
    setCurrentUser({});
    navigate("/");
  };

  console.log(currentUser);

  return (
    <ul className="menuProfile">
      <li>
        <NavLink to="/profile">Mon profil</NavLink>
      </li>
      <li>
        <button type="button" onClick={handleLogOut}>
          Déconnexion
        </button>
      </li>
    </ul>
  );
}

我不会回答您的问题,只提供翻译。如果您需要进一步的帮助,请随时告诉我。

英文:

I would like to create a log out with useContext - ReactJS. For this, I would like to empty my useContext.

This is my App.jsx:

import { BrowserRouter as Router, Routes, Route } from &quot;react-router-dom&quot;;
import { useState } from &quot;react&quot;;
import { ToastContainer } from &quot;react-toastify&quot;;
import &quot;react-toastify/dist/ReactToastify.css&quot;;
import HeaderAuth from &quot;./components/templates/header-auth&quot;;
import Header from &quot;./components/templates/header&quot;;
import Login from &quot;./components/authentification/login&quot;;
import Register from &quot;./components/authentification/register&quot;;
import Password from &quot;./components/authentification/password&quot;;
import HomePage from &quot;./components//homePage&quot;;
import Profile from &quot;./components/authentification/profile&quot;;
import Footer from &quot;./components/templates/footer&quot;;
import &quot;./app.scss&quot;;

// CONTEXTS
import User from &quot;./contexts/userContext&quot;;

export default function App() {
  const [user, setUser] = useState(null);

  return (
    &lt;main id=&quot;App&quot; className=&quot;App&quot;&gt;
      &lt;ToastContainer position=&quot;top-left&quot; autoClose={5000} /&gt;
      &lt;User.Provider value={{ user, setUser }}&gt;
        &lt;Router&gt;
          {user ? &lt;Header /&gt; : &lt;HeaderAuth /&gt;}
          &lt;Routes&gt;
            &lt;Route exact path=&quot;/&quot; element={&lt;Login /&gt;} /&gt;
            &lt;Route exact path=&quot;/register&quot; element={&lt;Register /&gt;} /&gt;
            &lt;Route
              exact
              path=&quot;/login/identity/forgotten-password&quot;
              element={&lt;Password /&gt;}
            /&gt;
            &lt;Route exact path=&quot;/hp&quot; element={&lt;HomePage /&gt;} /&gt;
            &lt;Route exact path=&quot;/profile&quot; element={&lt;Profile /&gt;} /&gt;
          &lt;/Routes&gt;
          &lt;Footer /&gt;
        &lt;/Router&gt;
      &lt;/User.Provider&gt;
    &lt;/main&gt;
  );
}

And this is what I tried to code -> my dropdown where I put the log out button:

import { NavLink } from &quot;react-router-dom&quot;;
import { useNavigate } from &quot;react-router-dom&quot;;
import { useContext } from &quot;react&quot;;

// CONTEXT
import User from &quot;../../../contexts/userContext&quot;;

export default function Menu() {
  const [currentUser, setCurrentUser] = useContext(User);
  const navigate = useNavigate();

  const handleLogOut = () =&gt; {
    setCurrentUser({});
    navigate(&quot;/&quot;);
  };

  console.log(currentUser);

  return (
    &lt;ul className=&quot;menuProfile&quot;&gt;
      &lt;li&gt;
        &lt;NavLink to=&quot;/profile&quot;&gt;Mon profil&lt;/NavLink&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;button type=&quot;button&quot; onClick={handleLogOut}&gt;
          D&#233;connexion
        &lt;/button&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  );
}

I don't know why, but I get this error:

React – 登出 UseContext

Do you have any idea why I get this error?

Thanks a lot !

答案1

得分: 1

将这一行更改为:

const { currentUser, setCurrentUser } = useContext(User);

你之所以会收到错误是因为你试图将上下文值解构为一个数组,但它实际上是一个对象。

英文:

Change this line:
const [ currentUser, setCurrentUser ] = useContext(User);

To this one:
const { currentUser, setCurrentUser } = useContext(User);

You are getting that error because you are trying to destructure the context value as an array, but it's an object.

答案2

得分: 0

你将上下文(ctx)解构为一个数组。

将你的 Menu 组件的第一行替换为以下内容:

const { currentUser, setCurrentUser } = useContext(User);
英文:

You destructure the ctx as an array.

Replace the first line of your Menu component with the following

const { currentUser, setCurrentUser } = useContext(User);

huangapple
  • 本文由 发表于 2023年3月3日 22:15:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定