‘export ‘default’ (imported as ‘Cookies’) was not found in ‘react-cookie’ (possible exports unknown)

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

export 'default' (imported as 'Cookies') was not found in 'react-cookie' (possible exports unknown)

问题

我在尝试使用'react-cookie'从浏览器中访问cookie时遇到了上面的错误。

这是我正在尝试的代码:

import Cookies from 'react-cookie';

function App() {

  const { currentUser } = useContext(AuthContext);

  const [token, setToken] = useState(null);

  useEffect(() => {
    const tokenFromCookie = Cookies.get("accessToken");
    setToken(tokenFromCookie);
  }, []);
英文:

I am getting above error while trying to access cookie from browser using 'react-cookie'.

Here is the code i am trying:

    import Cookies from 'react-cookie';
    
    function App() {
    
      const { currentUser } = useContext(AuthContext);
    
      const [token, setToken] = useState(null);
    
      useEffect(() => {
        const tokenFromCookie = Cookies.get("accessToken");
        setToken(tokenFromCookie);
      }, []);

答案1

得分: 1

这个对象 Cookies 被导入以与 CLASS 组件一起使用

import { withCookies, Cookies } from 'react-cookie';

而对于函数组件,可以像这样使用 useCookies

import { useCookies } from 'react-cookie';

function App() {

  const { currentUser } = useContext(AuthContext);
  const [cookies, setCookie] = useCookies(['accessToken']);
  
  // 这部分不是很有用,因为您可以使用 useCookies 访问 cookie
  // const [token, setToken] = useState(null);

  // useEffect(() => {
  //   const tokenFromCookie = cookies.accessToken;
  //   setToken(tokenFromCookie);
  // }, []);
}
英文:

this object Cookies is imported to use with CLASS Component

import { withCookies, Cookies } from 'react-cookie';

and for Function Component use useCookies like this

/// here the error
    import {useCookies} from 'react-cookie';
    
    function App() {
    
      const { currentUser } = useContext(AuthContext);
      const [cookies, setCookie] = useCookies(['accessToken']);
      
      // this part aren't usefull because u have access to the cookies using useCookies
      // const [token, setToken] = useState(null);
    
      // useEffect(() => {
      //   const tokenFromCookie = cookies.accessToken;
      //   setToken(tokenFromCookie);
      // }, []);

huangapple
  • 本文由 发表于 2023年7月17日 13:56:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701809.html
匿名

发表评论

匿名网友

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

确定