Skip to content

Commit c6e3bbf

Browse files
authored
Merge pull request #1094 from japgolly/lonely-useEffect
Actually invoke init in components stuck in "first"
2 parents 1b98dcb + 8066700 commit c6e3bbf

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ object HookComponentBuilder {
2828
new ComponentPC.First(ctx => init(ctx.props))
2929

3030
override def render(f: P => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.None]): Component[P, s.CT] =
31-
ScalaFn(f)
31+
ScalaFn{ p =>
32+
init(p)
33+
f(p)
34+
}
3235

3336
override def renderWithReuseBy[A](reusableInputs: P => A)(f: A => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.None], r: Reusability[A]): Component[P, s.CT] =
3437
customBy(p => CustomHook.shouldComponentUpdate(f).apply(() => reusableInputs(p)))

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,23 @@ object HooksTest extends TestSuite {
573573
protected implicit def hooksExt1[Ctx, Step <: HooksApi.AbstractStep](api: HooksApi.Primary[Ctx, Step]): X_UseEffect_Primary[Ctx, Step]
574574
protected implicit def hooksExt2[Ctx, CtxFn[_], Step <: HooksApi.SubsequentStep[Ctx, CtxFn]](api: HooksApi.Secondary[Ctx, CtxFn, Step]): X_UseEffect_Secondary[Ctx, CtxFn, Step]
575575

576+
def testSingle(): Unit = {
577+
val counter1 = new Counter
578+
val counter2 = new Counter
579+
def state() = s"${counter1.value}:${counter2.value}"
580+
581+
val comp = ScalaFnComponent.withHooks[Unit]
582+
.X_useEffect(counter1.incCB.ret(counter2.incCB))
583+
.X_useEffect(counter1.incCB(101))
584+
.X_useEffect(counter1.incCB.ret(counter2.incCB))
585+
.render(_ => EmptyVdom)
586+
587+
test(comp()) { _ =>
588+
assertEq(state(), "103:0")
589+
}
590+
assertEq(state(), "103:2")
591+
}
592+
576593
def testConst(): Unit = {
577594
val counter1 = new Counter
578595
val counter2 = new Counter
@@ -1292,6 +1309,7 @@ object HooksTest extends TestSuite {
12921309
"useDebugValue" - testUseDebugValue()
12931310
"useEffect" - {
12941311
import UseEffect._
1312+
"single" - testSingle()
12951313
"const" - testConst()
12961314
"constBy" - testConstBy()
12971315
"deps" - testWithDeps()
@@ -1302,6 +1320,7 @@ object HooksTest extends TestSuite {
13021320
"useForceUpdate" - testUseForceUpdate()
13031321
"useLayoutEffect" - {
13041322
import UseLayoutEffect._
1323+
"single" - testSingle()
13051324
"const" - testConst()
13061325
"constBy" - testConstBy()
13071326
"deps" - testWithDeps()

0 commit comments

Comments
 (0)