Skip to content

Commit 43d7634

Browse files
authored
Merge pull request #1001 from japgolly/topic/fncomp-with-reuse
Add ScalaFnComponent withReuse methods
2 parents 42c49d6 + a175e3b commit 43d7634

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

coreGeneric/src/main/scala/japgolly/scalajs/react/component/ScalaFn.scala

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import japgolly.scalajs.react.internal._
55
import japgolly.scalajs.react.vdom.VdomNode
66
import japgolly.scalajs.react.{Children, CtorType, PropsChildren, facade}
77
import scala.scalajs.js
8+
import japgolly.scalajs.react.Reusability
89

910
object ScalaFn {
1011

@@ -23,6 +24,11 @@ object ScalaFn {
2324
.mapUnmounted(_.mapUnmountedProps(_.unbox))
2425
}
2526

27+
@inline def withHooks[P] =
28+
HookComponentBuilder.apply[P]
29+
30+
// ===================================================================================================================
31+
2632
def apply[P](render: P => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.None]): Component[P, s.CT] =
2733
create[P, Children.None, s.CT](b => render(b.unbox))(s)
2834

@@ -32,6 +38,17 @@ object ScalaFn {
3238
def justChildren(render: PropsChildren => VdomNode): Component[Unit, CtorType.Children] =
3339
create(b => render(PropsChildren(b.children)))
3440

35-
@inline def withHooks[P] =
36-
HookComponentBuilder.apply[P]
41+
// ===================================================================================================================
42+
43+
def withReuse[P](render: P => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.None], r: Reusability[P]): Component[P, s.CT] =
44+
withHooks[P].renderWithReuse(render)(s, r)
45+
46+
def withReuseBy[P, A](reusableInputs: P => A)(render: A => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.None], r: Reusability[A]): Component[P, s.CT] =
47+
withHooks[P].renderWithReuseBy(reusableInputs)(render)(s, r)
48+
49+
def withChildrenAndReuse[P](render: (P, PropsChildren) => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.Varargs], rp: Reusability[P], rc: Reusability[PropsChildren]): Component[P, s.CT] =
50+
withHooks[P].withPropsChildren.renderWithReuse(i => render(i.props, i.propsChildren))
51+
52+
def withChildrenAndReuse[P, A](reusableInputs: (P, PropsChildren) => A)(render: A => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.Varargs], r: Reusability[A]): Component[P, s.CT] =
53+
withHooks[P].withPropsChildren.renderWithReuseBy(i => reusableInputs(i.props, i.propsChildren))(render)
3754
}

doc/changelog/2.0.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ You can run the script in the Migration section at the bottom of the page to aut
363363
* Add:
364364
* `AsyncCallback.debounce(duration): AsyncCallback[Unit]`
365365
* `Callback.debounce(duration): Callback`
366+
* `ScalaFnComponent.withReuse{,By}`
367+
* `ScalaFnComponent.withChildrenAndReuse{,By}`
366368
* `ReactTestUtils` is now a `trait` as well as an `object` so that you can mix it into your own test utils collection
367369
* Upgrade deps
368370
* Cats-effect to 3.2.9

0 commit comments

Comments
 (0)