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

78
Views
Passing value up in react

I have a component with a lot of buttons, which increment the counter. I need the counter all the way up in the main component, so I want to pass the Counter value;

import { useState } from "react";
import Card from "./Card";

const ProductList = (props) => {
    const products = props.products;
    const [ Counter, setCounter ] = useState(0);
    const Count = n => { setCounter(v => v + n) }

    return ( 
        <div className="ProductList" >
            {products.map((product) => (
                <Card product={product} key={product.id} C={Count} />))}
        </div>
    );
}

export default ProductList;

up to the parent component:

import { useEffect, useState } from "react";
import ProductList from "./ProductList";

const Products = () => {
    const [products, setProducts] = useState (null);

    useEffect (() => {
        fetch('http://localhost:8000/products')
            .then(res => {
                return res.json();
            })
            .then(data => {
                setProducts(data);
            })
    }, []);
    
    return ( 
        <div className="ProductList">
            {products && <ProductList products={products}/>}
        </div>
    );
}

export default Products;

It's a simple question, but I'm not sure how it works. Can anyone help? Thanks!

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

0

Define the counter state in the parent component then pass both the counter and setCounter to the child as props. Update counter with setCounter and it will be update in the parent component also.

Child:

import Card from "./Card";

const ProductList = ({counter, setCounter}) => {
    const products = props.products;
    const Count = n => { setCounter(v => v + n) }

    return ( 
        <div className="ProductList" >
            {products.map((product) => (
                <Card product={product} key={product.id} C={Count} />))}
        </div>
    );
}

export default ProductList;

Parent:

import ProductList from "./ProductList";

const Products = () => {
    const [products, setProducts] = useState (null);
    const [counter, setCounter] = useState(0);

    useEffect (() => {
        fetch('http://localhost:8000/products')
            .then(res => {
                return res.json();
            })
            .then(data => {
                setProducts(data);
            })
    }, []);
    
    return ( 
        <div className="ProductList">
            {products && 
         <ProductList 
             counter={counter} 
             setCounter={setCounter}
             products={products}
        />}
        </div>
    );
}

export default Products;
about 4 years ago · Juan Pablo Isaza Report

0

If you need the Counter reference in the Products component you should define it in the component itself and pass the function reference down to the child component.

Try to change your code like this:

Products component:

const Products = () => {
  const [products, setProducts] = useState(null);
  const [Counter, setCounter] = useState(0);
  
  const count = (n) => {
    setCounter((v) => v + n);
  };

  useEffect(() => {
    fetch('http://localhost:8000/products')
      .then((res) => {
        return res.json();
      })
      .then((data) => {
        setProducts(data);
      });
  }, []);

  return (
    <div className='ProductList'>
      {products && <ProductList products={products} count={count}/>}
    </div>
  );
};

export default Products;

ProductList component

import Card from "./Card";

const ProductList = (props) => {
    const products = props.products;
    const count = props.count
    
    return ( 
        <div className="ProductList" >
            {products.map((product) => (
                <Card product={product} key={product.id} C={count} />))}
        </div>
    );
}

export default ProductList;

However, if you need to pass the function down multiple levels, you should probably use the Context API, but from the question, I presume you just need to go down one level.

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!