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

95
Views
useStates seem as undefined on props

I am trying to get some datas from child to parent. There is a way I usually do, and it totally works. But for one page I used:

 <Link to={{
            pathname: `/productBuy`,
            state: { product, iconClick }
          }}>

and when I send another prop from App.js to productBuy page, it's shown under product and it's undefined.

Codes from App.js :

const [productInformation, setProductInformation] = useState([]);
<Route path="/productBuy" render={props => <ProductBuy {...props} productInformation {productInformation} setProductInformation={setProductInformation} />} />

productBuy.js :

const ProductBuy = (productInfo, {productInformation,setProductInformation}) => {


    return ( 
        <div className="productBuy">
            <div className="productBuyContainer">
                <ProductLeft productInfo={productInfo.location.state} />
                <ProductRight productInfo={productInfo.location.state} productInformation={productInformation} setProductInformation={setProductInformation}/>
            </div>
        </div>
     );

}

When I console.log, my props are shown under product object as undefined. and when I invoke a function, an error appears: ProductRight.js:51 Uncaught TypeError: setProductInformation is not a function

Is there a way to solve this problem?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

First of all you're missing the = after productInformation in the render prop:

<ProductBuy {...props} productInformation={productInformation} setProductInformation={setProductInformation} />

And the second issue is that you're unpacking the props incorrectly. Both productInformation and setProductInformation are available in the props argument (the first positional argument) in your function, but you're unpacking it in the second argument instead:

// INCORRECT
const ProductBuy = (productInfo, {productInformation,setProductInformation}) => { ... }

You can unpack it from the productInfo argument, which is an object that holds all the props:

const ProductBuy = (productInfo) => {
    const { productInformation, setProductInformation } = productInfo;
    return ( 
        <div className="productBuy">
            <div className="productBuyContainer">
                <ProductLeft productInfo={productInfo.location.state} />
                <ProductRight productInfo={productInfo.location.state} productInformation={productInformation} setProductInformation={setProductInformation}/>
            </div>
        </div>
     );
}

You can also choose to unpack it at the top level:

const ProductBuy = ({ location, productInformation, setProductInformation }) => {
    return ( 
        <div className="productBuy">
            <div className="productBuyContainer">
                <ProductLeft productInfo={location.state} />
                <ProductRight productInfo={location.state} productInformation={productInformation} setProductInformation={setProductInformation}/>
            </div>
        </div>
     );
}
about 4 years ago · Juan Pablo Isaza Report

0

Add equal sign when passing the productInformation to props seems you forgot that in App.js

<Route path="/productBuy" render={props => <ProductBuy {...props} productInformation={productInformation} setProductInformation={setProductInformation} />} />
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!