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';
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.
If the setter of state is unused, you can avoid it the useState
. You can keep the value in a const (in the body of the component), or move outside of the component.
setState
is used when it's needed to be aware of the changes in the value stored in state. This way the component knows when to re-render.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With