Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useReused hook as a monadic way of accessing CustomHook.reusableDeps #1102

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,27 @@ trait extra {
HookResult(UseRef.unsafeCreateToJsComponent(a))

/**
* Returns a stateful value, and a function to update it.
*
* During the initial render, the returned state is the same as the value passed as the first
* argument (initialState).
*
* During subsequent re-renders, the first value returned by useState will always be the most recent
* state after applying updates.
*/
* Returns a stateful value, and a function to update it.
*
* During the initial render, the returned state is the same as the value passed as the first
* argument (initialState).
*
* During subsequent re-renders, the first value returned by useState will always be the most recent
* state after applying updates.
*/
@inline final def useStateWithReuse[S: ClassTag: Reusability](
initialState: => S
): HookResult[UseStateWithReuse[S]] =
HookResult(UseStateWithReuse.unsafeCreate(initialState))

/**
* Given a reusable value, returns the original value that is being reused whenver reusability
* applies, together with a revisition a number that increments only when the value isn't reused.
*
* Useful for using `Reusability` logic in facades to JS hooks that accept dependencies: you can
* pass the revision as a dependency to the JS hook.
*/
@inline final def useReused[D: Reusability](deps: => D): HookResult[(D, Int)] =
CustomHook.reusableDeps[D].toHookResult(() => deps)

}
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,29 @@ object HooksTest extends TestSuite {
}
}

def testFromFunction() = {
private def testUseReused(): Unit = {
implicit val reusePIByRounding: Reusability[PI] = Reusability.by(_.pi / 2)

val comp = ScalaFnComponent[Unit] { _ =>
for {
count <- useState(PI(0))
reused <- useReused(count.value)
(stable, rev) = reused
} yield
<.div(
<.div(s"count=${count.value}, stable=$stable, rev=$rev"),
<.button(^.onClick --> count.modState(_ + 1))
)
}

test(comp()) { (t) =>
t.assertText("count=PI(0), stable=PI(0), rev=1")
t.clickButton(1); t.assertText("count=PI(1), stable=PI(0), rev=1")
t.clickButton(1); t.assertText("count=PI(2), stable=PI(2), rev=2")
}
}

private def testFromFunction() = {
val jsHook1: js.Function1[Int, Int] = _ + 1
val jsHook2: js.Function2[Int, Int, String] = (a: Int, b: Int) => (a + b).toString

Expand Down Expand Up @@ -2210,6 +2232,7 @@ object HooksTest extends TestSuite {
"renderWithReuse (monadic alternative using Render.memo)" - {
"main" - testMonadicRenderWithReuse()
}
"useReused" - testUseReused()
"fromFunction" - testFromFunction()
}
}
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.10.6
Loading