Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created February 25, 2023 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howmanysmall/af81c040024f5cd824c19620d9b90e1c to your computer and use it in GitHub Desktop.
Save howmanysmall/af81c040024f5cd824c19620d9b90e1c to your computer and use it in GitHub Desktop.
local ReactRoblox = require("ReactRoblox")
local function mount(element: any, parent: any, key: string?): RoactHandle
if parent ~= nil and typeof(parent) ~= "Instance" then
error(string.format(
"Cannot mount element (`%s`) into a parent that is not a Roblox Instance (got type `%s`) \n%s",
if element then tostring(element.type) else "<unknown>",
typeof(parent),
if parent ~= nil then inspect(parent) else ""
))
end
local root
if _G.__ROACT_17_COMPAT_LEGACY_ROOT__ then
root = ReactRoblox.createLegacyRoot(Instance.new("Folder"))
else
root = ReactRoblox.createRoot(Instance.new("Folder"))
end
if parent == nil then
parent = Instance.new("Folder")
parent.Name = "Target"
end
if key == nil then
if _G.__ROACT_17_COMPAT_LEGACY_ROOT__ then
key = "ReactLegacyRoot"
else
key = "ReactRoot"
end
end
if _G.__ROACT_17_INLINE_ACT__ then
ReactRoblox.act(function()
root:render(ReactRoblox.createPortal({ [key] = element }, parent))
end)
else
root:render(ReactRoblox.createPortal({ [key] = element }, parent))
end
return {
root = root,
parent = parent,
key = key :: string,
}
end
local function unmount(roactHandle)
if _G.__ROACT_17_INLINE_ACT__ then
ReactRoblox.act(function()
roactHandle.root:unmount()
end)
else
roactHandle.root:unmount()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment