Skip to content
This repository has been archived by the owner on Nov 21, 2017. It is now read-only.

Is there a JSX equivalent to this.props.children? #13

Open
MattMcFarland opened this issue Jun 16, 2015 · 4 comments
Open

Is there a JSX equivalent to this.props.children? #13

MattMcFarland opened this issue Jun 16, 2015 · 4 comments

Comments

@MattMcFarland
Copy link

Hi,

I've switched from React to Mithril, and MSX has been really great. With JSX, you can invoke {this.props.children} to render child components that can be between an opening and closing tag.

<MyComponent>
    <MyHeader>{this.props.heading}</MyHeader>
    <SomeContent>
        <AnotherCustomComponent/>
        <AnotherComponent>
            <ReallyNestedNow/>
        </AnotherComponent>
    </SomeContent>
</MyComponent>

So how does it work?

SomeContent () {
    render () {
        <div>
            <header>
                <h4>Lorem Ipsum</h4>
            </header>
            {this.props.children} 
            // In this example, it would render every component inbetween the opening and closing <SomeContent> tag as seen above.
            <footer>Lalalalalal</footer>
        </div>
    }
}

This makes working with JSX very easy, especially when running into situations where it makes more sense to enclose a component this way.

I'm running into a sitaution with Mithril where I want to do this, but it is not working. Is there any alternative?

Thanks!!
Matt

@insin
Copy link
Owner

insin commented Jun 17, 2015

MSX will bundle any child contents into an array which is passed as the third argument to a Mithril component's view function, so something like this should work:

var SomeContent = {
  view(ctrl, props, children) {
    <div>
      <header>
        <h4>Lorem Ipsum</h4>
      </header>
      {children} 
      <footer>Lalalalalal</footer>
    </div>
  }
}

Note that MSX will be deprecated as soon as the next version of Mithril is released, as a change has been made which allows you to make full use of Babel's JSX transpiler instead. Babel will pass any children as additional arguments, so you'd need to use this signature instead to put children into an array.

view(ctrl, props, ...children)

@MattMcFarland
Copy link
Author

Thanks for your reply. It is good to know that we can use babel as well. I am currently building my application with browserify, and I tried babelify but it made everything look like react. So I'm using mithrilify right now.

Question, why do you have view(ctrl, props, children) - why not (ctrl, children) ??? where do props come in??

@stephenwf
Copy link

They are when using the component itself. So using the example above:

var SomeOtherComponent = {
  view(ctrl, props, children) {
    return <div>
      <SomeComponent someproperty="some value">
        <input type="text" />
      </SomeComponent>
    </div>
  }
}

Now props.someproperty can be used in "SomeComponent". Just from the logical order that they are used I guess is the reason.

@dead-claudia
Copy link

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

No branches or pull requests

4 participants