admin管理员组

文章数量:1355522

In react you can do something like:

var Wrap = React.createClass({
    render: function() {
        return <div>{ this.props.children }</div>;
    }
});

var App = React.createClass({
    render: function() {
        return <Wrap><h1>Hello word</h1></Wrap>;
    }
});

This allows you to pass in a ponent to another. But what if Wrap had another div that you could put content into. So consider the following:

var Wrap = React.createClass({
    render: function() {
        return (
          <div>
            <ul className="icons">
             // Another poent should be passed here from App to render icons.
            </ul>
            { this.props.children }
          </div>
        );
    }
});

var App = React.createClass({
    render: function() {
        return <Wrap><h1>Hello word</h1></Wrap>;
    }
});

In the above example you can see that not only do I want to pass in children of the App ponent but I also want to pass another ponent that is icons for the ul section. Is this possible?

If so, how?

In react you can do something like:

var Wrap = React.createClass({
    render: function() {
        return <div>{ this.props.children }</div>;
    }
});

var App = React.createClass({
    render: function() {
        return <Wrap><h1>Hello word</h1></Wrap>;
    }
});

This allows you to pass in a ponent to another. But what if Wrap had another div that you could put content into. So consider the following:

var Wrap = React.createClass({
    render: function() {
        return (
          <div>
            <ul className="icons">
             // Another poent should be passed here from App to render icons.
            </ul>
            { this.props.children }
          </div>
        );
    }
});

var App = React.createClass({
    render: function() {
        return <Wrap><h1>Hello word</h1></Wrap>;
    }
});

In the above example you can see that not only do I want to pass in children of the App ponent but I also want to pass another ponent that is icons for the ul section. Is this possible?

If so, how?

Share Improve this question edited Oct 9, 2014 at 15:19 phtrivier 13.4k9 gold badges52 silver badges82 bronze badges asked Oct 9, 2014 at 3:14 SeekingTruthSeekingTruth 1,0543 gold badges15 silver badges23 bronze badges 1
  • Aren't these two different ponents? <Icons />, which returns the <ul>, and whatever goes inside { this.props.children}. Then in App you can instantiate two different ponents rather than one. I think you'll continue to feel like you're fighting React if you pass ponents as props. – Ross Allen Commented Oct 9, 2014 at 22:37
Add a ment  | 

2 Answers 2

Reset to default 4

I see two options:

  1. Pass the other ponent as prop, e.g.

    <Wrap icons={<Icons />}><h1>...</h1></Wrap>
    
  2. Pass two children two Wrap and render each of them in the appropriate places, e.g.

    <Wrap>
      <Icons />
      <h1>...</h1>
    </Wrap>
    

Using Higher order ponents to do this is cleaner than accessing children by index or looping imo.

const wrap = (Icons) => class extends React.Component {
    render() {
        return (
          <div>
            <ul className="icons">
                <Icons {...this.props.iconProps} />
            </ul>
            { this.props.children }
          </div>
        );
    }
});

class AppIcons extends React.Component {
    render: function() {
        return <div />
    }
});

class App extends React.Component {
    render: function() {
        return <Wrap iconProps={this.props}><h1>Hello word</h1></Wrap>;
    }
});

const Wrap = wrap(AppIcons);


ReactDOM.render(App, domContainerNode)

本文标签: javascriptrendering multiple children of different types in reactStack Overflow