How to access a browser cookie in a react app

后端 未结 4 710
你的背包
你的背包 2021-02-10 20:43

This question is a bit popular but Im not having such luck. Im mostly a backend person so Im learning as I go along.

I have a cookie named connect.sid and v

4条回答
  •  被撕碎了的回忆
    2021-02-10 21:36

    Full example using the react-cookie v2.

    You need to wrap your root component in and also the individual components where you want to access the cookies in withCookies(..). The cookies are then part of props.

    client.js:

    import {CookiesProvider} from "react-cookie";
    
    
      
    
    

    App.js:

    import React from "react";
    import {withCookies} from 'react-cookie';
    
    class App extends React.Component {
      render() {
        const {cookies} = this.props;
        return 

    {cookies.get('mycookie')}

    } } export default withCookies(App);

提交回复
热议问题