Search code examples
scalajs-react

Use StateSnapshots with a Backend in scalajs-react


The main component in the StateSnapshot example does not use a backend, but I need one. My try:

class MainBackend($ : BackendScope[Unit, Name]) {
  def render(name: Name) = {
    val firstNameV = StateSnapshot.zoomL(Name.firstName).of(name)
    val surnameV = StateSnapshot.zoomL(Name.surname).of(name)
    <.div(
      <.label("First name:", NameChanger(firstNameV)),
      <.label("Surname:", NameChanger(surnameV)),
      <.p(s"My name is ${name.surname}, ${name.firstName} ${name.surname}.")
    )
  }
}

val Main = ScalaComponent
  .builder[Unit]("StateSnapshot example")
  .initialState(Name("John", "Wick"))
  .renderBackend[MainBackend]
  .build

I get the compilation error value firstName is not a member of object app.TreeView.Name in the row val surnameV = StateSnapshot.zoomL(Name.surname).of(name).


Solution

  • Solved at a GitHub issue by using .setStateVia($) instead of .of($).