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

144
Views
Does the parent useEffect trigger if the parent's state is changed in the child component?

I'm trying to pass the setter of the parent component to the child component but when I invoked the setter it didn't trigger the parent's useEffect.

const Product = ({ product }) => {
return (
    <div className="border border-black rounded border-2 p-5 flex m-5">
        <div className="w-1/3">
          <p>Name: <span>{product.name}</span></p>
        </div>
        <div className="">
          <p>Price: <span>{product.price}</span></p>
        </div>
    </div>
 )
}

const CreateForm = ({addProduct}) => {
     //renders a POST form for adding product when submit is clicked it triggers 
     //addProduct
}

const Main () => {
   const [products, setProducts] = useState([])
   const fetchData () => {
      fetch("get/products")
      .then(response => response.json())
      .then(data => setProducts(data))

   }

   useEffect(() => {
      console.log("useEffect triggered!")
      fetchData();
   }, [])
   
   const addProduct = (product) => {
      const updatedProducts = [...products, product]
      setProducts(updatedProducts)
   }

   const data = products.map(product => <Product product={product}/>)

   return (
    <div className="m-10">
      <CreateForm addProduct={addProduct}/>

      {data}
    </div>
    )
}

When the submit button is clicked it performs a POST requests creating a product and the addProduct is invoked

about 4 years ago · Juan Pablo Isaza
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!