mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
38 lines
813 B
Text
38 lines
813 B
Text
local React = require("../../React")
|
|
local ReactRoblox = require("../../ReactRoblox")
|
|
|
|
type RenderedHook<Props, Result> = {
|
|
result: Result,
|
|
rerender: (Props?) -> (),
|
|
unmount: () -> (),
|
|
}
|
|
|
|
local function renderHook<Props, Result>(callback: (Props) -> Result, props: Props?): RenderedHook<Props, Result>
|
|
local root = ReactRoblox.createLegacyRoot(Instance.new("Folder"))
|
|
local hook = {}
|
|
|
|
local function Component(props: Props)
|
|
hook.result = callback(props)
|
|
end
|
|
|
|
function hook.rerender(newProps: Props?)
|
|
props = newProps or props
|
|
ReactRoblox.act(function()
|
|
root:render(React.createElement(Component, props))
|
|
end)
|
|
end
|
|
|
|
function hook.unmount()
|
|
ReactRoblox.act(function()
|
|
root:unmount()
|
|
end)
|
|
end
|
|
|
|
ReactRoblox.act(function()
|
|
hook.rerender(props)
|
|
end)
|
|
|
|
return hook
|
|
end
|
|
|
|
return renderHook
|