3

Whenever I use useState or a similar function with the same structure, I receive a lint error if I only need to use of the array functions, for example:

const [searchParams, setSearchParams] = useSearchParams();

If I only need to use setSearchParams, but not searchParams, lint will show an error that the first one is never used.

Replacing searchParams with null or undefined will throw other errors instead. Is there a way to fix this in the syntax? (Without changing lint configuratios)

sir-haver
  • 3,096
  • 7
  • 41
  • 85
  • Not sure why you are not using `searchParams`, but maybe you can replace it with `_`? `const [_, setSearchParams] = useSearchParams();` since `_` is a convention to mean "I won't use this variable", or omit it completely – evolutionxbox Sep 14 '22 at 10:22
  • 1
    To be honest it's a little weird that you would need only the `setState` function but not the actual state value. However, if it is justified, you can write `const [, setSearchParams] = useSearchParams();`. This way the first member of the array ( `searchParams` ) will be ignored. – tomleb Sep 14 '22 at 10:23
  • Thanks that's what I was looking for! Yes it's temporary not using the first one – sir-haver Sep 14 '22 at 10:24

1 Answers1

7

If you want to use just searchParams, use this:

const [searchParams] = useSearchParams();

If you want to use just setSearchParams, use this:

const [, setSearchParams] = useSearchParams();
Ali Nauman
  • 1,374
  • 1
  • 7
  • 14