Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Created June 24, 2024 11:13
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(path.to.React)
local useState = React.useState
local useEffect = React.useEffect
local function useAttribute(target: Instance, attributeName: string)
local value, setValue = useState(target:GetAttribute(attributeName))
useEffect(function()
local connection = target:GetAttributeChangedSignal(attributeName):Connect(function()
setValue(target:GetAttribute(attributeName))
end)
return function()
connection:Disconnect()
end
end, {})
return value
end
return useAttribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment