Children
Declares children of an instance when paired with New()
Type | Since | Scoped |
---|---|---|
Declaration | 0.0.1 | No |
Constuctor
[Children] = {Instance} | Computed
Usage
Children is used as in index in the New
constructor to declare the children of said instance. It’s used like this:
local Object = New("Object", {
Property = Value,
Property2 = Value2,
[Children] = {
... -- Array of other objects. Supports usage of New() in here as well
}
})
If you wanted children to be more reactive/dynamic, you can alternatively pass in a Computed()
state. When the computed instance updates, it will update the children. For this to work, Computed()
must return a table of instances every time.
local Names = Value({"Bob", "Rob", "Billy", "Bobby"})
local DynamicChildren = Computed(function(Use)
local CreatedChildren = {}
for Index, Name in Use(Names) do
table.insert(CreatedChildren, New("TextLabel", {
Text = Name,
-- And let's pretend there are properties
}))
end
return CreatedChildren
end)
New(ExistingFrame, {
[Children] = DynamicChildren
})
-- At first, there will be four children, each a text label that corresponds to each value in the Names state
-- Then, we change the value, doing the same thing and deleting the old children
Names.Value = {"Jon"} -- When we call this, there will now only be a singular text label, with the text "Jon"