Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unused var in React Hooks

I'm using React Hooks in my SPA. I know how it works, but I have a doubt when declaring the hook, i.e., I'm using react-cookie and the declaration of the hook is const [cookies, setCookie, removeCookie] = useCookies([]);.

In my case, I will only need the removeCookie var, I won't use the cookies and setCookie vars in my functional page, so lint complains about unused vars.

Is it possible to ignore these two vars? I tried const [..., ..., removeCookie] = useCookies([]); but this won't work.

Thanks in advance.

like image 983
Otavio Bonder Avatar asked Jan 15 '20 14:01

Otavio Bonder


People also ask

Can we get previous value in useEffect?

Only after the render is the useEffect call within the usePrevious Hook updated with the new value, 1 . This cycle continues, and in this way, you'll always get the previous value passed into the custom Hook, usePrevious .

Do React hooks return a value?

React hooks are not required to return anything. The React documentation states that: We don't have to return a named function from the effect. We called it cleanup here to clarify its purpose, but you could return an arrow function or call it something different.

Why React hooks are use full?

Hooks make React so much better because you have simpler code that implements similar functionalities faster and more effectively. You can also implement React state and lifecycle methods without writing classes.


1 Answers

Since you use array destructuring to make the assignments, you can ignore the items by adding commas without the variables/consts names:

const [,, removeCookie] = useCookies([]);

Example:

const [,, c] = [1, 2, 3]

console.log(c)
like image 122
Ori Drori Avatar answered Sep 19 '22 02:09

Ori Drori