Last active
September 4, 2024 13:30
-
-
Save cxmeel/29ff06edb04c74c36218a7f4b1a75334 to your computer and use it in GitHub Desktop.
React Luau hooks
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
local React = require("@pkgs/React") | |
local useState = React.useState | |
local useEffect = React.useEffect | |
local function useAttribute<T>( | |
target: Instance, | |
attributeName: string, | |
fallbackValue: T? | |
): T? | |
local value, setValue = useState(target:GetAttribute(attributeName)) | |
useEffect(function() | |
local signal = target | |
:GetAttributeChangedSignal(attributeName) | |
:Connect(function() | |
setValue(target:GetAttribute(attributeName)) | |
end) | |
return function() | |
signal:Disconnect() | |
end | |
end, { target, attributeName } :: { any }) | |
return value or fallbackValue | |
end | |
return useAttribute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment