Skip to content

Commit acde662

Browse files
authored
Merge pull request #1113
Fix #1112: Hooks not being initialised with props children
2 parents bf4d1cf + 7f9148c commit acde662

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

library/coreGeneric/src/main/scala/japgolly/scalajs/react/hooks/HookComponentBuilder.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,14 @@ object HookComponentBuilder {
120120
step.next(init, f)
121121

122122
override def render(f: Ctx => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.Varargs]): Component[P, s.CT] =
123-
ScalaFn.withChildren((p: P, pc: PropsChildren) => f(HookCtx.withChildren(p, pc)))
123+
ScalaFn.withChildren((p: P, pc: PropsChildren) => {
124+
val h = HookCtx.withChildren(p, pc)
125+
init(h)
126+
f(h)
127+
})
124128

125129
def render(f: (P, PropsChildren) => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.Varargs]): Component[P, s.CT] =
126-
ScalaFn.withChildren(f)
130+
render((ctx: Ctx) => f(ctx.props, ctx.propsChildren))
127131

128132
override def renderWithReuseBy[A](reusableInputs: Ctx => A)(f: A => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.Varargs], r: Reusability[A]): Component[P, s.CT] =
129133
customBy(ctx => CustomHook.shouldComponentUpdate(f).apply(() => reusableInputs(ctx)))

library/tests/src/test/scala/japgolly/scalajs/react/core/HooksTest.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,19 @@ object HooksTest extends TestSuite {
12881288
}
12891289
}
12901290

1291+
private def testOnMountWithPropsChildren(): Unit = {
1292+
var text = "uninitialised"
1293+
val comp = ScalaFnComponent.withHooks[Unit]
1294+
.withPropsChildren
1295+
.useEffectOnMount(Callback { text = "ok" })
1296+
.render(_.propsChildren)
1297+
1298+
test(comp(123)) { t =>
1299+
assertEq(text, "ok")
1300+
t.assertText("123")
1301+
}
1302+
}
1303+
12911304
// ===================================================================================================================
12921305

12931306
override def tests = Tests {
@@ -1316,6 +1329,7 @@ object HooksTest extends TestSuite {
13161329
"depsBy" - testWithDepsBy()
13171330
"mount" - testOnMount()
13181331
"mountBy" - testOnMountBy()
1332+
"mountWithPropsChildren" - testOnMountWithPropsChildren()
13191333
}
13201334
"useForceUpdate" - testUseForceUpdate()
13211335
"useLayoutEffect" - {

0 commit comments

Comments
 (0)