Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Last active September 4, 2024 13:30
Show Gist options
  • Save cxmeel/29ff06edb04c74c36218a7f4b1a75334 to your computer and use it in GitHub Desktop.
Save cxmeel/29ff06edb04c74c36218a7f4b1a75334 to your computer and use it in GitHub Desktop.
React Luau hooks
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