question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Statefull sub-component example

See original GitHub issue

After going through documentation and examples, I was stumped over how to build react components in scala that included a Backend and could be used conveniently by parent components as none of the sub-components included Backends. https://github.com/tastejs/todomvc/tree/master/examples/scalajs-react provided a good mechanism but that example is a little bit much, it might be better to have a shorter example like the one below included in your examples.

  object Counter {

    case class Props(name: String, inc: () => Callback)
    case class State(count: Int)

    class Backend($:  BackendScope[Props, State]) {

      def increment():Callback = $.modState(s => State(s.count+1))
      }

      def render(P: Props, S:State): ReactElement = {
        <.div(
          ^.input(
            ^.`type` := "button",
            ^.value := "+",
            ^.onClick --> { P.inc().runNow() ; increment() }
          ),
          s" ${P.name}: ${S.count}"
        )
      }
    }

    val component = ReactComponentB[Props]("Counter")
      .initialState_P(p => State(0))
      .renderBackend[Backend]
      .build

    def apply( s:String, inc: () => Callback): ReactElement = component(Props(s,inc))
  }

  object TotalCounters {

    case class Props(title: String)
    case class State(total: Int)

    class Backend($:  BackendScope[Props, State]) {
      
      def incTotal() = $.modState(s => State(s.total+1))

      def render(P: Props, S:State): ReactElement = {
        <.div(
          <.h1(P.title),
          Counter("John",incTotal),
          Counter("Fred",incTotal),
          Counter("Jack",incTotal),
          <.input(
            ^.`type` := "button",
            ^.value := "Reset total",
            ^.onClick --> $.setState(State(0))
          ),
          s" Total: ${S.total}"
        )
      }
    }

    val component = ReactComponentB[Props]("Counters")
      .initialState_P(p => State(0))
      .renderBackend[Backend]
      .build

    def apply( title: String ): ReactElement = component(Props(title))
  }

So, if you are interested in a PR I can create one, but if you have no interest in adding that then there’s no point me creating the PR. If you would be interested in a PR, then I probably need to fix some things in this example than I am doing wrong and instruction would be welcome.

If the neo branch was going to be ready fairly soon then it might not be worth your time.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
japgollycommented, Aug 21, 2016

You remind me that I was going to publish usage guidelines ages ago but forgot. I even wrote up a heap of stuff. Even better though would be something collaborative where community can contribute patterns/suggestions and multiple people suggest pros/cons, highlight trade-offs etc. That’d be cool.

0reactions
japgollycommented, Feb 14, 2017

While I still haven’t gotten around to publishing general usage guidelines and gotchas, I have done so for Callback recently seeing as it caused some confusion. Anyway this is resolved so closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dagger 2 on Android: the shiny new @Component.Factory
This is how the ApplicationComponent would look like: And here's how we build it: Again, we can use the same idea on subcomponents...
Read more >
Stateful and stateless components, the missing manual
When something is “stateful”, it is a central point that stores information in memory about the app/component's state. It also has the ability ......
Read more >
Converting stateful React component to stateless functional ...
I have written a small stateful React component. When this component loads, in componentDidMount method I am making use of Kendo UI to...
Read more >
How To Share State Across React Components with Context
In this tutorial, you'll share state across multiple components using React context. React context is an interface for sharing information ...
Read more >
Subcomponents - Dagger
Subcomponents are components that inherit and extend the object graph of a parent component. You can use them to partition your application's object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found