reactjsreact-cookie

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


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);
      }, []);


Solution

  • 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);
          // }, []);