Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass a child function to a component #130

Closed
SalomonBrys opened this issue Aug 10, 2019 · 6 comments
Closed

How to pass a child function to a component #130

SalomonBrys opened this issue Aug 10, 2019 · 6 comments

Comments

@SalomonBrys
Copy link

I'm trying to use an external component that I have imported as such:

@file:JsModule("react-transition-group")
package react_transition_group

import react.RClass

external val Transition : RClass<dynamic>

This component expects a function child (see its doc here: http://reactcommunity.org/react-transition-group/transition).

So, I can use it, but how do I give it a child function?

Transition {
    // How do I pass a child function?>
}

I've tried childList += { trState: String -> ... } but this yields a React error: "Invariant Violation: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead."

@Leonya
Copy link
Collaborator

Leonya commented Aug 10, 2019

Use RProps instead of dynamic. Then, simply,

Transition {
  div {
    ...
  }
}

@SalomonBrys
Copy link
Author

This doesn't change the problem.

I don't want to add a component child to the Transition component (in your example, a div), but a function child.

@Skolotsky
Copy link
Contributor

Here's example how to add functional component in terms of kotlin react #121

@SalomonBrys
Copy link
Author

Not applicable.
What's needed is a Value Prop, not a Functional Component.
In this instance, this is a function that takes a Boolean parameter, which is a primitive and cannot implement RProp.

@SalomonBrys
Copy link
Author

SalomonBrys commented Aug 20, 2019

This works:

fun <P> RBuilder.renderChild(rc: RBuilder.(P) -> Unit) {
    childList += { p: P -> buildElement { rc(p) } }
}

And can be used as such:

Transition {
    attrs {
        `in` = shown
        timeout = 350
    }

    renderChild { transitionState: String ->
        div { /*...*/ }
    }
}

This should be added to the library.

@Skolotsky
Copy link
Contributor

{ p: P -> buildElement { rc(p) } } is functional component, but you create new each time.
Better way is to create once and reuse it. And you can see in example #121 creating and reusing fuctional component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants