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

201
Views
How to pass index to child function

Here is the question. I want to pass index of each child element to the function handleChange, but I can't get it))) When I click on TabPanelItem appears error handleChange is not a function What's the problem?

Parent Element:

const TabPanel = () => {

    const tabsPanelData = [
        {label: 'tab1'},
        {label: 'tab2'},
        {label: 'tab3'}
    ];

    const [tabIndex, settabIndex] = useState(0);

    const handleChange = (index) => {
        console.log(index);
    }


    return (
        <StyledTabPanel>
            
            {tabsPanelData.map((tabPanelItem, i) => {
                const {label, handleChange} = tabPanelItem;
                return (
                    <TabPanelItem
                    key={i}
                    label={label}
                    handleChange={handleChange(i)}
                  />
                )
            })}
        </StyledTabPanel>
    )
}

and my child component:

const TabPanelItem = ({ label, handleChange }) => {


    return (
      <StyledTabPanelItem
      
      onClick={handleChange}>
         {label}
      </StyledTabPanelItem>
    )
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you made a mistake in this line

const {label, handleChange} = tabPanelItem;

you dont handleChange in the tablePanelItem

it should be

const {label} = tabPanelItem;

and you are executing the function handleChange by doing this

  <TabPanelItem
       key={i}
       label={label}
       handleChange={handleChange(i)}
/>

you either pass the handleChange function as the body of an arrow function as the props of the TabPanelItem

<TabPanelItem
   key={i}
   label={label}
   handleChange={() => handleChange(i)}
/>

then on click that arrow function would be executed

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!