0

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

0

1 Answer 1

1

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.