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

209
Views
React - passing props to parent

I can't seem to get it to work. all i need is to pass a single value, selectedCategory. the error i'm getting:

Cannot read properties of undefined (reading 'passSelectedCategory') (in the child component)

Child:

const CategoryWindow =(props)=> {
    const categories = ["h", "a", "s", "o"]

    return (
        <div className="categories-div">
                {categories.map(category => 
                    <button className={"category-btn " + `${category}`}
                        key={category}
                        value={category}
                        onClick={props.passSelectedCategory(selectedCategory)}       <----
                    >
                            <h2>{category}</h2>
                    </button>
                    )}
                
        </div>
    )
}

parent:


const Main =()=> {
    const [showElement, setShowElement] = useState([1][0][0]);
    const [selectedCategory, setSelectedCategory] = useState();

    return (
    <>
        <main className="main">
            <div>
                <div>
                     <CategoryWindow
                         passSelectedCategory={setSelectedCategory}   <---
                     />
                </div>
                .....  

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

 <button className={"category-btn " + `${category}`}
                        key={category}
                        value={category}
                        onClick={() => props.passSelectedCategory(category)}
                    >
                            <h2>{category}</h2>
                    </button>

Everything in your code is perfect you just need to change your onClick function as I mentioned above.

about 4 years ago · Santiago Trujillo Report

0

Try to pass category as a parameter at the props.passSelectedCategory ( the child component, CategoryWindow doesn't have the visibility of the selectedCategory variable)

about 4 years ago · Santiago Trujillo 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!