I'm sending token from my API and storing it in the cookie inside the browser. I want to access that token with my frontend app using react. Can somebody tell me how to do it
1 Answer
you have a libary in react: react-cookie but js-cookie is better.
react-cookie: https://www.npmjs.com/package/react-cookie
js-cookie: https://www.npmjs.com/package/js-cookie
you can get your cookies when you need example: By using a useEffect or onClick.
example:
import React, { useState } from 'react';
import Cookies from 'js-cookie';
function App() {
const [cookies, setCookies] = useState({
username: Cookies.get('username')
})
}
-
I tried this but somehow it's always undefined. When i look in storage cookie is there but in console shows undefined. Commented Nov 14, 2022 at 15:21
-
const [cookies, setCookies] = useState({ token: Cookies.get("jwt"), }) it is stored under the name jwt Commented Nov 14, 2022 at 15:24
-