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
Asked
Active
Viewed 1,343 times
1 Answers
0
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')
})
}

Faranix
- 11
- 2
-
I tried this but somehow it's always undefined. When i look in storage cookie is there but in console shows undefined. – Djela Srca Nov 14 '22 at 15:21
-
const [cookies, setCookies] = useState({ token: Cookies.get("jwt"), }) it is stored under the name jwt – Djela Srca Nov 14 '22 at 15:24
-
Send your code, i check this. – Faranix Nov 15 '22 at 19:29