Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use AND (&&) OR ( || ) operators in dependency array React JS

Can I use && and/or || operators in dependency array like this:

const isVisible = true
const isModified = false

useEffect(() => console.log("both are true"), [isVisible && isModified])

Is it a bad practice? Eslint says React Hook useEffect has a complex expression in the dependency array. react-hooks/exhaustive-deps

How can I achieve the same result by doing this the right way?

like image 304
Andrew Avatar asked Oct 20 '25 09:10

Andrew


1 Answers

Eslint says lots of stuff. If this is your project and you like that style, change your .eslintrc so that it doesn't flag that. If you are coding to someone else's style, then you should respect it and maybe do:

const isVisible = true
const isModified = false

const combined = [isVisible && isModified]

useEffect(() => console.log("both are true"), combined)
like image 102
Jason Kennaly Avatar answered Oct 23 '25 01:10

Jason Kennaly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!