react-use icon indicating copy to clipboard operation
react-use copied to clipboard

OR / AND operator hooks!

Open codemilli opened this issue 3 years ago • 2 comments

Hello, I need a hook that has a boolean value with OR / AND operator. Like below

useOR

const [a, setA] = useState(false)
const [b, setB] = useState(false)
return { value: a || b, setA, setB }

useAND

const [a, setA] = useState(false)
const [b, setB] = useState(false)
return { value: a && b, setA, setB }

I think these are good micro utility hook for solving diverse state problem. But I cannot name it, I wanna name it with good metaphor like useToggle.

Is anybody has a idea or know already existing solution, please let me know. Thanks !

codemilli avatar Sep 01 '22 12:09 codemilli

Can you give an example of usage of the api you want?

childrentime avatar Sep 02 '22 05:09 childrentime

@childrentime

Thanks for the interest.

I have some business logics the use or state like below.

const useIsUserActive = () => {
  const { value, setA, setB } = useOR();
  
  // when user is in page
  setA(isUserInPage);
  // when user mouse moved in 30 seconds.
  setB(hasUserMoved);
  
  // isUserActive
  return value;
}

codemilli avatar Sep 02 '22 13:09 codemilli