Skip to content

Load and save cookies within your React application

License

Notifications You must be signed in to change notification settings

vizeat/react-cookie

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-cookie

Load, save and remove cookies within your React application

Isomorphic cookies!

You can also plug it directly with a Node.js request by adding just before the renderToString: var unplug = reactCookie.plugToRequest(req, res);
(require the cookieParser middleware)

To ensure long running async operations do not attempt to alter cookies after the request has been sent, call the unplug function that is returned in a finally block in your router.

If you are within a non-browser or Node.js environment, you can use reactCookie.setRawCookie(req.headers.cookie)

Download

NPM: npm install react-cookie
Bower: bower install react-cookie
CDN: https://cdnjs.cloudflare.com/ajax/libs/react-cookie/0.4.3/react-cookie.min.js

Examples

import { Component } from 'react';
import cookie from 'react-cookie';

export default class MyApp extends Component {
  constructor(props) {
    super(props);

    this.state =  { userId: cookie.load('userId') };
  }

  onLogin(userId) {
    this.setState({ userId });
    cookie.save('userId', userId, { path: '/' });
  }

  onLogout() {
    cookie.remove('userId', { path: '/' });

    /** Clear all cookies starting with 'session' (to get all cookies, omit regex argument) */
    Object.keys(cookie.select(/^session.*/i)).forEach(name => cookie.remove(name, { path: '/' }))
  }

  render() {
    return (
      <LoginPanel onSuccess={this.onLogin.bind(this)} />
    );
  }
}

Without CommonJS

You can use react-cookie with anything by using the global variable reactCookie.

Note that window need to exists to use reactCookie.

Usage

reactCookie.load(name, [doNotParse])

reactCookie.select([regex])

reactCookie.save(name, val, [opt])

reactCookie.remove(name, [opt])

reactCookie.plugToRequest(req, res): unplug()

reactCookie.setRawCookie(cookies)

opt

Support all the cookie options from the RFC 6265.

path

cookie path

expires

absolute expiration date for the cookie (Date object)

maxAge

relative max age of the cookie from when the client receives it (seconds)

domain

domain for the cookie

secure

true or false

httpOnly

true or false

License

This project is under the MIT license. You are free to do whatever you want with it.

About

Load and save cookies within your React application

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%