Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
react recursive accordion component throwing an error each child in a list should have a unique key prop

I'm writing custom nested recursive accordion component to display objects, In that component I'm getting an error each child in a list should have a unique key prop, not sure where I need to put the key property

how to resolve the same.

accordion.js:

import React, { useState, useCallback } from 'react'
import { Container, Grid } from '@material-ui/core'
import './styles.css'


function Accodian({ explorer }) {
    const [ expand, setExpand ] = useState(false)
    const handleExpand = useCallback(() => {
        setExpand(prevState => !prevState)
    })
    if (explorer.children) {
        return (
            <div style={{ width: '100%' }} className={'tabs'} key={explorer.label}>
                {explorer.children ? (
                    <>
                        <div className='tab' onClick={handleExpand}>
                            <label className={ expand ? 'tab-label-expanded' : 'tab-label' }>
                                {explorer.label}
                            </label>
                        </div>
                        {expand ? (
                            <Container className='tab-content'>
                                <Grid container spacing={1}>
                                    {explorer.children.map(child => {
                                        return (
                                            <React.Fragment>
                                                {Array.isArray(child.children) ? (
                                                    <Accodian explorer={child}/>
                                                ) : (
                                                    <Grid item xs={12} sm={6} md={6} lg={4} xl={3}>
                                                        <li>
                                                            <b>{child.label}</b>{' '}: {child.value}
                                                        </li>
                                                    </Grid>
                                                )}
                                            </React.Fragment>
                                        )
                                    })}
                                </Grid>
                            </Container>
                        ) : null}
                    </>
                ) : null}
            </div>
        )
    } else {
        return <div style={{ paddingLeft: '20px' }}>{explorer.label}</div>
    }
}

export default Accodian
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After did many analysis found the place where we can able to apply the key in the component

Solution: <React.Fragment key={child.label}>

import React, { useState, useCallback } from 'react'
import { Container, Grid } from '@material-ui/core'
import './styles.css'


function Accodian({ explorer }) {
    const [ expand, setExpand ] = useState(false)
    const handleExpand = useCallback(() => {
        setExpand(prevState => !prevState)
    })
    if (explorer.children) {
        return (
            <div style={{ width: '100%' }} className={'tabs'} key={explorer.label}>
                {explorer.children ? (
                    <>
                        <div className='tab' onClick={handleExpand}>
                            <label className={ expand ? 'tab-label-expanded' : 'tab-label' }>
                                {explorer.label}
                            </label>
                        </div>
                        {expand ? (
                            <Container className='tab-content'>
                                <Grid container spacing={1}>
                                    {explorer.children.map(child => {
                                        return (
                                            <React.Fragment key={child.label}> // applied key
                                                {Array.isArray(child.children) ? (
                                                    <Accodian explorer={child}/>
                                                ) : (
                                                    <Grid item xs={12} sm={6} md={6} lg={4} xl={3}>
                                                        <li>
                                                            <b>{child.label}</b>{' '}: {child.value}
                                                        </li>
                                                    </Grid>
                                                )}
                                            </React.Fragment>
                                        )
                                    })}
                                </Grid>
                            </Container>
                        ) : null}
                    </>
                ) : null}
            </div>
        )
    } else {
        return <div style={{ paddingLeft: '20px' }}>{explorer.label}</div>
    }
}

export default Accodian
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!