Skip to content

Commit b2182f6

Browse files
authored
Pass child elements in as a third argument to component's body (#4)
1 parent a7af1f1 commit b2182f6

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

froact.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ local function newList(roact: any)
146146
end
147147
end
148148

149+
local blankChildren = table.freeze({})
150+
149151
type HookFunction<Props, Hooks> = (
150-
render: (Props, Hooks) -> any,
152+
render: (Props, Hooks, { Element }) -> any,
151153
options: any
152154
) -> any
153155

@@ -158,12 +160,18 @@ local function newC<Hooks>(
158160
)
159161
return function<Props>(
160162
config: ComponentConfig,
161-
body: (Props, Hooks) -> any
163+
body: (Props, Hooks, { Element }) -> any
162164
): (Props, Children) -> any
163165
local isPure = if config.pure == nil
164166
then not unpureByDefault
165167
else config.pure
166-
local Component = hooks(body, {
168+
-- Wrap the body to have children passed in as a third argument
169+
local function wrappedBody(props: any, hooks)
170+
local children = props[roact.Children]
171+
props[roact.Children] = nil
172+
body(props, hooks, if children then children else blankChildren)
173+
end
174+
local Component = hooks(wrappedBody, {
167175
componentType = if isPure then "PureComponent" else "Component",
168176
name = if config.name then config.name else "Component",
169177
})

froactful.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ local function newList(roact: any)
147147
end
148148
end
149149

150+
local blankChildren = table.freeze({})
151+
150152
type HookFunction<Props, Hooks> = (
151-
render: (Props, Hooks) -> any,
153+
render: (Props, Hooks, { Element }) -> any,
152154
options: any
153155
) -> any
154156

@@ -159,12 +161,18 @@ local function newC<Hooks>(
159161
)
160162
return function<Props>(
161163
config: ComponentConfig,
162-
body: (Props, Hooks) -> any
164+
body: (Props, Hooks, { Element }) -> any
163165
): (Props, Children) -> any
164166
local isPure = if config.pure == nil
165167
then not unpureByDefault
166168
else config.pure
167-
local Component = hooks(body, {
169+
-- Wrap the body and pass children in
170+
local function wrappedBody(props: any, hooks)
171+
local children = props[roact.Children]
172+
props[roact.Children] = nil
173+
body(props, hooks, if children then children else blankChildren)
174+
end
175+
local Component = hooks(wrappedBody, {
168176
componentType = if isPure then "PureComponent" else "Component",
169177
name = if config.name then config.name else "Component",
170178
})

0 commit comments

Comments
 (0)