1

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 Jayasekara
  • 1,945
  • 1
  • 6
  • 26
Ali Husnain
  • 35
  • 1
  • 7

3 Answers3

4

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?

Greg
  • 5,862
  • 1
  • 25
  • 52
2

...or pass this issue.

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

  /* tslint:disable:no-unused-variable */
Rukshan Jayasekara
  • 1,945
  • 1
  • 6
  • 26
0

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
  • 2,186
  • 1
  • 13
  • 27
  • `const [_, setCookie] = useCookies(["user_token"])` I believe `_` is a normal var name in JS, and linters will probably complaing about it as well. – Greg 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