Bootstrap with React: accordion won't work

2019-07-20 02:46发布

I am trying to get a bootstrap accordion running, where my panels are React classes. Somehow this doesn't work:

<ReactBootstrap.Accordion>
   <WontWorkPanel pkey={1} />
   <WontWorkPanel pkey={2} />
</ReactBootstrap.Accordion>

http://jsfiddle.net/3azxcquh/

The WontWorkPanel is React class that renders the single panel with the key this.props.pkey .

Could someone explain me what I'm doing wrong, or how to do it better?

Thanks!

1条回答
够拽才男人
2楼-- · 2019-07-20 03:15

The Accordion clones its children with new props, and those props control the showing/hiding of the Panel component. To allow that to still work with a custom Panel wrapper, you need to transfer props from the wrapper to the Panel child:

Fiddle: http://jsfiddle.net/ssorallen/3azxcquh/6/

var WontWorkPanel = React.createClass({
  render: function() {
    return this.transferPropsTo( 
      <ReactBootstrap.Panel header={"WontWork " + this.props.key} key={this.props.key}>
        Anim pariatur cliche reprehenderit, enim eiusmod high life
        accusamus terry richardson ad squid. 3 wolf moon officia aute,
        non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt
        laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua pu
      </ReactBootstrap.Panel>
    );
  }
});
查看更多
登录 后发表回答