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.

React Binding to ObservableState

See original GitHub issue

Actually, to bind my ObservableState to a React component, I’m wrapping it into a div:

div(store) {
  react {
    awesomeExternalReactComponent {
          (...)
    }
  }
}

I tried to workaround this extra div but, unfortunately, using the builtin extension bind(store, true, factory) doesn’t quite work as expected since the React widget is not a Container and we’re ending up with duplicated react components on every state update since removeChildren is ignored:

// Homemade version, trying to skip the extra <div>
fun <S> Container.react(
        state: ObservableState<S>,
        classes: Set<String>? = null,
        className: String? = null,
        builder: RBuilder.(S) -> Unit): Widget {

    return react(classes = classes, className = className) {
        builder(state.value)
    }.bind(state, true) { // remove children *NOT* working
        react(classes = classes, className = className) {
            builder(it)
        }
    }
}

Is that the expected behavior or can it be filled as a bug?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
rjaroscommented, Oct 8, 2020

I think there is no need to use bind for React component, because it already supports state management. You just need to connect your component to your store. You can do this with a single line:

store.subscribe { react.state = it }

Or you can create a DSL builder function like this:

fun <S> Container.reactBind(      // <- we need new name to avoid conflict
    state: ObservableState<S>,
    classes: Set<String>? = null,
    className: String? = null,
    builder: RBuilder.(getState: () -> S, changeState: ((S) -> S) -> Unit) -> Unit
): React<S> {
    val react = React(state.getState(), classes ?: className.set, builder)
    state.subscribe { react.state = it }
    this.add(react)
    return react
}

And you can use it:

            val store = ObservableValue("Some text")
            vPanel {
                reactBind(store) { getState, _ ->
                    div {                           // <- this is ReactJS component, not KVision
                        +getState()
                    }
                }
                button("Add a dot").onClick {
                    store.value += "."
                }
            }

I’ll add this DSL function to the next KVision release.

0reactions
rjaroscommented, Oct 9, 2020

Fixed in 3.16.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to bind React state to RxJS observable stream?
Update. To abstract out some of the below complexity, use recompose's mapPropsStream or componentFromStream. E.g.
Read more >
bind(observable) - React-RxJS
Binds an Observable factory function to React, and returns a hook and shared stream representing the created Observables. Copy. function bind<A extends unknown ......
Read more >
How to bind React state to RxJS observable stream?-Reactjs
[Solved]-How to bind React state to RxJS observable stream?-Reactjs. Search. score:2. Accepted answer. One option could be to use Rx.Observable.
Read more >
RxJS with React Hooks for state management - LogRocket Blog
An Observer consumes the data supplied by an Observable. In our demo app, we'll be using our setState Hook to consume data from...
Read more >
React integration - MobX
As a rule of thumb, use MobX observables when the state captures domain data that is shared among components (including children). Such as...
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