Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

React / Binding.scala / html.scala Interoperability

License

NotificationsYou must be signed in to change notification settings

Atry/ReactToBindingHtml.scala

Repository files navigation

Scala CIreacttobindinghtml Scala version supportbindinghtmltoreact Scala version supportbindingreacttoreact Scala version supportScaladoc

This repository includes adapters forReact /Binding.scala /html.scala interoperability.

Motivation

The rendering process in React components is unpredictable, resulting in unnecessary reevaluation and, even worse, unexpected reevaluation of side effects, if any. Precise data-binding inBinding.scala is a fast and predictable alternative to React's repeatedly re-rendering approach.

However, currently there are more third-party components in the React ecosystem than Binding.scala. It would be nice if a web developer could reuse React components while taking advantage of Binding.scala's precise data-binding. This repository includes the following adapters for reusing React components in Binding.scala + html.scala web apps:

With the help of the adapters, you will be able to reuse existing React components while getting rid of React hooks orsetState by managing your app's states in Binding.scala.

Getting Started

// build.sbtlibraryDependencies++=Seq("com.yang-bo"%%%"html"% (if (scalaBinaryVersion.value=="3")"3+"else"2+"),"com.yang-bo"%%%"bindingreacttoreact"%"1+","com.yang-bo"%%%"reacttobindinghtml"%"1+","com.yang-bo"%%%"bindinghtmltoreact"%"1+",)

The following example demonstrates how to use React components withhtml.scala's@html literal in Scala 2

importcom.thoughtworks.binding.Bindingimportcom.thoughtworks.binding.Binding._importcom.yang_bo.ReactToBindingHtml.Implicits._importcom.yang_bo.BindingHtmlToReact.Implicits._importcom.yang_bo.BindingReactToReact.Implicits._importorg.scalajs.dom._importorg.lrng.binding.htmlimportslinky.web._importslinky.web.html._importslinky.core.facade._@htmldefspinner(currentNumber:Var[Int]):ReactElement= {// virtual DOM span element  span(// real DOM button element<button      id="minus"      onclick={ (event:MouseEvent)=> currentNumber.value-=1 }>-</button>,// virtual DOM label element with Binding.scala's `.bind` magicBinding {      label(currentNumber.bind.toString)    },// virtual DOM button element    button(      id:="plus",      onClick:= { (event:Any)=> currentNumber.value+=1 }    )("+"    )  )}valcurrentNumber=Var(50)@htmlvalrootView= {// real DOM fieldset element  <fieldset>    <legend>I am an `@html` literal that contains aReact component    </legend>    { spinner(currentNumber) }  </fieldset>}html.render(documet.body, rootView)

For Scala 3 users, usehtml.scala'shtml"..." interpolation instead, as shown below:

importcom.thoughtworks.binding.Bindingimportcom.thoughtworks.binding.Binding._importcom.yang_bo.ReactToBindingHtml.Implicits._importcom.yang_bo.BindingHtmlToReact.Implicits._importcom.yang_bo.BindingReactToReact.Implicits._importcom.yang_bo.html._importorg.scalajs.dom._importslinky.web._importslinky.web.html._importslinky.core.facade._defspinner(currentNumber:Var[Int]):ReactElement= {// virtual DOM span element  span(// real DOM button elementhtml"""<button      onclick=${ (event:MouseEvent)=> currentNumber.value-=1 }    >-</button>""",// virtual DOM label element with Binding.scala's `.bind` magicBinding {      label(currentNumber.bind.toString)    },// virtual DOM button element    button(      id:="plus",      onClick:= { (event:Any)=> currentNumber.value+=1 }    )("+"    )  )}valcurrentNumber=Var(50)valrootView= {// real DOM fieldset elementhtml"""<fieldset>    <legend>      I am an html interpolation that contains a React component    </legend>${ spinner(currentNumber) }  </fieldset>"""}render(documet.body, rootView)

Even though adapters from this repository provide the flexibility to choose to create UI in either React or real DOM, to minimize the cost due to React rerendering,BindingReactToReact users are recommended to neither define any React components nor use any React hooks. Instead, to take the advantage of Binding.scala's precise data-binding, manage the application states in Binding.scala, and let theBindingReactToReact React components be instantiated implicitly, when using existing React components.

Because React virtual DOM does not support partial update provided by Binding.scala'sBindingSeq, create your own HTML UI as@html literals orhtml"..." interpolations, if the overhead due to React's virtual DOM differentiation matters.

Related tools

Adapters from this repository work with React types defined inSlinky by default. Other sources of React components need to be converted to Slinky types first.

  • React components defined inscalajs-react can be converted into Slinky types viatoSlinky, in order to use them in Binding.scala apps with the help of adapters from this repository.
  • React components defined in TypeScript can be converted to Slinky types viaScalablyTyped withFlavour.Slinky, in order to use them in Binding.scala apps with the help of adapters from this repository.

Links

About

React / Binding.scala / html.scala Interoperability

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp