All Projects β†’ tylerwolff β†’ useCookie

tylerwolff / useCookie

Licence: other
A React hook for managing cookies with no dependencies.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to useCookie

react-ua
πŸ“±React User Agent Component, Hook, and HOC. SSR-ready, full UT, using new React Context and Hooks API
Stars: ✭ 18 (-84.87%)
Mutual labels:  hooks
basic-component
Basic ui component based on React.
Stars: ✭ 11 (-90.76%)
Mutual labels:  hooks
git-toolkit
Gitε·₯具集
Stars: ✭ 35 (-70.59%)
Mutual labels:  hooks
react-808
808 Drum Machine built using React.js hooks API
Stars: ✭ 51 (-57.14%)
Mutual labels:  hooks
book-fullstack-react-with-typescript
Working through the code samples from Fullstack React with Typescript by Maksim Ivanov and Alex Bespoyasov
Stars: ✭ 52 (-56.3%)
Mutual labels:  hooks
material-ui-color
The lightest colorpicker, palette, colorinput, colorbutton ⚑ No dependencies. It uses React hooks, support Typescript theming and more !
Stars: ✭ 125 (+5.04%)
Mutual labels:  hooks
spiced-final-project
Career explorer platform developed in React.js in 6 days.
Stars: ✭ 14 (-88.24%)
Mutual labels:  cookies
react-reads
Recommended tools, curated articles to learn more about react-ecosystem and some common implementation logics in react,ts,next.
Stars: ✭ 19 (-84.03%)
Mutual labels:  hooks
use-double-tap
React hook for handling double tap on mobile devices
Stars: ✭ 18 (-84.87%)
Mutual labels:  hooks
amplify-material-ui
A Material-UI based implementation of aws amplify
Stars: ✭ 32 (-73.11%)
Mutual labels:  hooks
useSharedState
useSharedState is a simple hook that can be used to share state between multiple React components.
Stars: ✭ 0 (-100%)
Mutual labels:  hooks
laravel-react-spa
A Laravel-React SPA starter project template.
Stars: ✭ 94 (-21.01%)
Mutual labels:  hooks
react-hooks
Custom react hooks
Stars: ✭ 21 (-82.35%)
Mutual labels:  hooks
UniversalUnityHooks
A framework designed to hook into and modify methods in unity games via dlls
Stars: ✭ 78 (-34.45%)
Mutual labels:  hooks
react-ecommerce
E-commerce monorepo application using NextJs, React, React-native, Design-System and Graphql with Typescript
Stars: ✭ 136 (+14.29%)
Mutual labels:  hooks
useCustomHooks
πŸ“¦ npm package containing a set of custom hooks for your next React project.
Stars: ✭ 12 (-89.92%)
Mutual labels:  hooks
Portfolio-2020
Simple reactjs portfolio of Motasim Foad
Stars: ✭ 102 (-14.29%)
Mutual labels:  hooks
react-hooks-library
A collection of hooks and utilities for modern React
Stars: ✭ 236 (+98.32%)
Mutual labels:  hooks
cookies
Convenient way to use cookies with PSR-7
Stars: ✭ 17 (-85.71%)
Mutual labels:  cookies
KaufmannDigital.GDPR.CookieConsent
A ready-to-run package, that integrates an advanced cookie consent banner into your Neos CMS site.
Stars: ✭ 21 (-82.35%)
Mutual labels:  cookies

useCookie

npm package

A React hook for managing cookies with no dependencies.

Installation

npm install react-use-cookie

or

yarn add react-use-cookie

Usage

useCookie

import useCookie from 'react-use-cookie';

export default (props) => {
  const [userToken, setUserToken] = useCookie('token', '0');

  render(
    <div>
      <p>{userToken}</p>
      <button onClick={() => setUserToken('123')}>Change token</button>
    </div>
  );
};

In this example you can also set custom cookie options by passing an options object to setUserToken:

setUserToken('abcd', {
  days: 365,
  SameSite: 'Strict',
  Secure: true,
});

See setCookie for more option information about this.

As a convenience this package also exports the get and set functions so they can be used directly.

getCookie

If you need to access a cookie outside of a React component, you can use the exported getCookie function:

import { getCookie } from 'react-use-cookie';

const getUser = () => {
  const xsrfToken = getCookie('XSRF-TOKEN');
  // use to call your API etc
};

setCookie

If you need to set a cookie outside of a React component, you can use the exported setCookie function:

import { setCookie } from 'react-use-cookie';

const saveLocale = (locale) => {
  setCookie('locale', locale, {
    days: 1,
    domain: 'github.com',
    SameSite: 'Lax',
    Secure: true,
  });
};

You can also specify cookie options as a third argument:

{
  // The number of days the cookie is stored (defaults to 7)
  days?: number;

  // The path of the cookie (defaults to '/')
  path?: string;

  // Browser defaults unless set
  domain?: string;
  SameSite?: 'None' | 'Lax' | 'Strict';
  Secure?: boolean;
  HttpOnly?: boolean;
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].