japgolly / scalajs-react

Facebook's React on Scala.JS
https://japgolly.github.io/scalajs-react/
Apache License 2.0
1.64k stars 231 forks source link

Syntax to integrate `useContext` with context function in Scala 3 #1074

Open rpiaggio opened 2 years ago

rpiaggio commented 2 years ago

In our apps, we use React context to store global implicits; eg: logger, effect type-class instances, web client backends, etc.

I'm thinking it would be nice to have something like, leveraging Scala 3's context functions:

  ScalaFnComponent
    .withHooks[Props]
    .useImplicitContext(MyContext.ctx)
    .useEffectBy( props =>
      // Theres an implicit `MyContext` in scope here; ie. this is a `MyContext ?=> Callback`
    )
    .useState(0)
    .render( (props, state) =>
      // Theres an implicit `MyContext` in scope here; ie. this is a `MyContext ?=> Callback`
    )

WDYT @japgolly ?

japgolly commented 2 years ago

Ooo cool idea! I like it! Unfortunately there's no chance I'm gonna have time to implement it, but a PR would be welcomed