You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.8 KiB

4 years ago
  1. package com.example.playscalajsreact.component
  2. import com.example.playscalajsreact.model.MyGlobalState
  3. import japgolly.scalajs.react.vdom.VdomElement
  4. import japgolly.scalajs.react.Callback
  5. import japgolly.scalajs.react._
  6. import japgolly.scalajs.react.vdom.html_<^._
  7. import com.example.playscalajsreact.route.AppRouter
  8. import org.scalajs.dom
  9. import scala.scalajs.js
  10. import com.example.playscalajsreact.model.User
  11. import japgolly.scalajs.react.extra.StateSnapshot
  12. import com.softwaremill.quicklens._
  13. import monocle.macros.Lenses
  14. import com.example.playscalajsreact.model._
  15. import japgolly.scalajs.react.MonocleReact._
  16. object Middle {
  17. final case class Props(name: String, ss: StateSnapshot[Data]) {
  18. @inline def render: VdomElement = Comp(this)
  19. }
  20. implicit def reusability: Reusability[Props] =
  21. Reusability.derive
  22. final class Backend($ : BackendScope[Props, Unit]) {
  23. // Method 2: StateSnapshot.withReuse.zoomL.prepareViaProps
  24. // Notice that we're using a normal lens here instead of a Reusable[lens]
  25. private val ssStrFn =
  26. StateSnapshot.withReuse.zoomL(Data.str).prepareViaProps($)(_.ss)
  27. def render(p: Props): VdomElement = {
  28. // Method 1: ss.withReuse.zoomStateL
  29. val ssI: StateSnapshot[Int] = p.ss.zoomStateL(Data.reusableLens.int)
  30. // Method 2: StateSnapshot.withReuse.zoomL.prepareViaProps
  31. val ssS: StateSnapshot[String] =
  32. ssStrFn(p.ss.value)
  33. <.div(
  34. <.h3(p.name, "hello"),
  35. <.div("IntEditor: ", IntEditor(ssI))
  36. // <.div("TextEditor: ", TextEditor(ssS), ^.marginTop := "0.6em"))
  37. )
  38. }
  39. }
  40. val Comp = ScalaComponent
  41. .builder[Props]
  42. .renderBackend[Backend]
  43. .configure(Reusability.shouldComponentUpdate)
  44. .build
  45. def apply(_props: Props): VdomElement = { Comp(_props) }
  46. }