Created
August 4, 2022 07:29
-
-
Save marcolink/c0fda9989cdb5c73b52c0839f1a632a9 to your computer and use it in GitHub Desktop.
React hook for a reduced boolean value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from "react"; | |
function reduce(record: Record<string, boolean>): boolean { | |
return Object.keys(record).reduce((previousValue, currentValue) => { | |
if (!record[currentValue]) { | |
return false; | |
} | |
return previousValue; | |
}, true); | |
} | |
export const useAllTrue = (record: Record<string, boolean>) => { | |
const [state, setState] = useState(() => reduce(record)); | |
useEffect(() => { | |
setState(reduce(record)); | |
}, [record]); | |
return state; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: