英文:
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);
// }, []);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论