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

123
Views
How Do I Pass a Function Through A React Component For Data Sharing Between Parent and Child?

I have followed this tutorial (https://www.freecodecamp.org/news/pass-data-between-components-in-react/) to the best of my ability but I am still having trouble

I am getting the following error:

Uncaught TypeError: childToParent is not a function"

Here is the parent function:

function App() {
const childToParent =(value) => {console.log(value)}
...
return (
...
  <SelectHours value={state.selectedHour} childToParent={childToParent}></SelectHours>
...
)

and the child function:

export default function SelectHours(selectedHours, childToParent) {
....
return (
<Select
  labelId="select-demo"
  id='select-demo'
  defaultValue=""
  //onChange={event => handleChange(event.target.value, true)}
  value={workHours[0] ? workHours[0] : ""}
>
  {
    workHours.map((value, index) => {
      return (
        <MenuItem
          value={value ? value : ""}
          key={index}
          onClick={() => childToParent(value)}
        >
          {value}
        </MenuItem>
      )
    })
  }
</Select>
)
....
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You need to add your curly braces so react can tell its a prop.

export default function SelectHours({selectedHours, childToParent}) {
....
return (
<Select
  labelId="select-demo"
  id='select-demo'
  defaultValue=""
  //onChange={event => handleChange(event.target.value, true)}
  value={workHours[0] ? workHours[0] : ""}
>
  {
    workHours.map((value, index) => {
      return (
        <MenuItem
          value={value ? value : ""}
          key={index}
          onClick={() => childToParent(value)}
        >
          {value}
        </MenuItem>
      )
    })
  }
</Select>
)
....
}
about 4 years ago · Juan Pablo Isaza Report

0

must destruct props like this.

export default function SelectHours({selectedHours, childToParent})

or use props like this

    export default function SelectHours(props)
      ...
    onClick={() => props.childToParent(value)}

read about object destructuring in this article

https://dmitripavlutin.com/javascript-object-destructuring/

about 4 years ago · Juan Pablo Isaza Report

0

You need to have a handler for ChildToParent inside

function App() {
...
const childToParent =(value) => {console.log(value)}
return (
...
  <SelectHours value={state.selectedHour} childToParent={childToParent}></SelectHours>
...
)

the App component

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!