javascriptreactjsnpmuse-state

How do I avoid unused setState functions? Can React useState be created without a setter?


I'm currently reducing / removing npm warnings on a React site.

A large number of these warnings are caused by the setState function as seen below, being 'unused'.

const [state, setState] = useState('some state');

Which of the following would be a better way to remove these warnings? Or is there a better way to approach the issue?

1.

const[state] = useState('some state');
const state = 'some state';

Solution

  • If setState isn't being used at all, then it's a value that never changes so can be a constant (2.). You could probably move it out of the component as well.