1

i am using react cookie for storing the tokens in the cokies..can any one tell me..how to clear cookies when used logout..

import React, { Component } from 'react';
import { withCookies } from 'react-cookie';


class Dashboard extends Component {
    constructor(props) {
        super(props);
        this.state = {
        }
        this.logout = this.logout.bind(this)
    }
    logout = (e) => {

        cookies.remove('Token');
        window.location.href = '/';
    }

    render() {
        return (
            <div className="Dashboard">
                <div>Dashboard</div>
                <button onClick={this.logout}>Logout</button>
            </div>
        );
    }
}
export default withCookies(Dashboard);
2
  • how r u routing? are you using react-router? Commented May 4, 2020 at 3:11
  • window.location.href = '/'; i am using this Commented May 4, 2020 at 3:12

2 Answers 2

1

You need to return false:

logout = (e) => {

     cookies.remove('Token');
     window.location.href = '/';
     return false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

('cookies' is not defined ) response is like this
You need to import import { withCookies, Cookies } from 'react-cookie'; Also, most people use universial cookies or js-cookie since its simpler to use.
thanks very much james for the help....i got the solution for that thing...in another way....ill post the anwser now
0

I got the solution for these thing... i have imported instanceOf from the prop-type... and changing the name of the Cookies to cookies...its clearing cookies after using logout

import React, { Component } from 'react';
import { withCookies, Cookies } from 'react-cookie';
import { instanceOf } from 'prop-types';


class Dashboard extends Component {
    static propTypes = {
        cookies: instanceOf(Cookies).isRequired
    };

    logout = (e) => {
        const { cookies } = this.props;
        cookies.remove('Token');
        window.location.href = '/';
        // return false;
    }

    render() {
        return (
            <div className="Dashboard" token={this.state.token} >
                <div>Dashboard</div>
                <button onClick={this.logout}>Logout</button>
            </div>
        );
    }
}
export default withCookies(Dashboard);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.