react-bootstrap Accordion not collapsing with map

I have an array of items I would like to create Panels out of which would eventually be inserted into the Accordion.

In one file, I have:

var items =, index) {
  return <Content {...content}, key={index}/>

return (

In another file called Content I have:
  <Panel header={this.props.header} eventKey={this.props.key}>

When I have the Accordion and Panel in the same file, they work. But when I generate the Panel using map after splitting them into two files, it doesn’t seem to collapse.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here is the solution

  { =>
     <AcPanel key={} item={item} eventKey={} />

In AcPanel Class use {…this.props} for fetch all properties and assign values on your parent class like i used “eventKey={}”.
<Panel header={`Collapsible Group Item`} bsClass='class-name' {...this.props}>
content here

Method 2

For anyone using Accordion.Toggle, this is how I got the accordion to collapse on map.

In the parent component of the Accordion, add an activeKey value to the state. Create a function to handle the active key change that will be passed down during your map to each child component of the accordion. Be sure to bind that function to the parent component.

this.state = {
  items = []
  activeKey: ''

handleActiveKeyChange = activeKey => {
  if (this.state.activeKey === activeKey) {
    //allows us to close expanded item by clicking its toggle while open
    activeKey = -1
  this.setState({ activeKey })

Pass the active key state and function accordingly in your render method:
<Accordion activeKey={this.state.activeKey}>
  {, index) => (
    <Item eventKey={index} handleActiveKeyChange={this.handleActiveKeyChange} />

In your child (item) element’s Accordion toggle, add the passed function and props:
      onClick={() => this.props.handleActiveKeyChange(this.props.eventKey)}
      //+ icon here
  //card content here 

Method 3

I found a solution that lets you use a component in a separate file.

I figured the problem must be to do with the Accordion not being able to pass some props down to the Panel because they go to the wrapping component instead, so I tried collecting all the props that weren’t mine and passing them to the panel:

// make sure you have all your own props specified before ...props
// so that they can't mess up the panel.
const { body, ...props } = this.props

  <Panel {...props}>

If you want to specify any props on the Panel, make sure they go after the collected props so that they take precedence over any defaults:
const { body, ...props } = this.props

  <Panel {...props}

If you choose any props names the same as whatever Accordion is passing to Panel, you could clobber something and break it – if you’re concerned, it should be straightforward to follow the code and see what is being passed on.

Method 4

react-bootstrap seems to complain if the children of an Accordion are not Panels, and by wrapping Panel inside of my Content class I was doing just that.

To solve this problem I had to step away from React’s convention. Rather than creating a class Content, I created another js file rather than a react.js file. By treating this file like a normal js file, I was able to call on a function inside of this file which maps over each ‘this.state.contents’ and return a Panel object.

This whole file will return an array of Panel class.

Method 5

The easiest way to do it is making a condition inside the eventKey parameter.

 <Card key={}> 
      <Accordion.Toggle as={Button} variant="link" eventKey={index === 0 ? '0' : index}>
    <Accordion.Collapse eventKey={index === 0 ? '0' : index}>
             CARD BODY

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x