0

I am using const [cookie, setCookie] = useCookies(["user_token"]) cookies, I only want to use setCookie and not use cookie variable. and tslint is crashing build for unused cookie. how can I get only one setCookie or pass this issue.

Rukshan
  • 1,826
  • 1
  • 3
  • 23
Ali Husnain
  • 25
  • 1
  • 7

3 Answers3

3

What you're doing here is called "deconstructing", and it is possible to ignore first element like this:

[, setCookie] = useCookies(["user_token"])

See this answer for more explanation: Destructuring array get second value?

Grzegorz
  • 5,219
  • 1
  • 27
  • 48
2

...or pass this issue.

Add this line just before the line which causes the error:

  /* tslint:disable:no-unused-variable */
Rukshan
  • 1,826
  • 1
  • 3
  • 23
1

The return value of react hook you mentioned is a simple array, you can look at it as an array too.

const setCookie = useCookies(["user_token"])[1]

const [_, setCookie] = useCookies(["user_token"])
If you want to take this action a lot

For getting inspired, you can also take a look at package use-st8 which they bind this to return value, make different access action based on passed arguments

const count = useSt8(0)
// get the current state
count() 
// change the state with to the given value
count(5)
Amirhe
  • 1,727
  • 1
  • 8
  • 20
  • `const [_, setCookie] = useCookies(["user_token"])` I believe `_` is a normal var name in JS, and linters will probably complaing about it as well. – Grzegorz Oct 17 '21 at 11:25
  • I think any parameter name starting with _ is exempt from the check. but you can also disable the check for the line or hole project if configs are more strict – Amirhe Oct 17 '21 at 15:49