Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Remove children when updating a host node to an element with nil children #210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Roact Changelog

## Unreleased

* Fixed an issue where updating a host element with children to an element with `nil` children caused the old children to not be unmounted. ([#210](https://github.com/Roblox/roact/pull/210))

## [1.0.0](https://github.com/Roblox/roact/releases/tag/v1.0.0)
This release significantly reworks Roact internals to enable new features and optimizations.

Expand Down
4 changes: 2 additions & 2 deletions src/RobloxRenderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function RobloxRenderer.updateHostNode(reconciler, virtualNode, newElement)
end

local children = newElement.props[Children]
if children ~= nil then
if children ~= nil or oldProps[Children] ~= nil then
reconciler.updateVirtualNodeWithChildren(virtualNode, virtualNode.hostObject, children)
end

Expand All @@ -280,4 +280,4 @@ function RobloxRenderer.updateHostNode(reconciler, virtualNode, newElement)
return virtualNode
end

return RobloxRenderer
return RobloxRenderer
27 changes: 27 additions & 0 deletions src/RobloxRenderer.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,33 @@ return function()
expect(message:find("RobloxRenderer%.spec")).to.be.ok()
end)
end)

it("should delete instances when reconciling to nil children", function()
local parent = Instance.new("Folder")
local key = "Some Key"

local element = createElement("Frame", {
Size = UDim2.new(1, 0, 1, 0),
}, {
child = createElement("Frame"),
})

local node = reconciler.createVirtualNode(element, parent, key)

RobloxRenderer.mountHostNode(reconciler, node)

expect(#parent:GetChildren()).to.equal(1)

local instance = parent:GetChildren()[1]
expect(#instance:GetChildren()).to.equal(1)

local newElement = createElement("Frame", {
Size = UDim2.new(0.5, 0, 0.5, 0),
})

RobloxRenderer.updateHostNode(reconciler, node, newElement)
expect(#instance:GetChildren()).to.equal(0)
end)
end)

describe("unmountHostNode", function()
Expand Down