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

93
Views
Update one property in MongoDB and update UI without refresh the page

I am trying to update one property Like I have a few properties, product name, price, quantity, supplier, and description. I am sending all the updated quantities with all properties to MongoDb, in that case, I am able to update the database and UI without any refresh.

const handleDelivered = (id) => {
        
        const newQuantity = (quantity - 1);
        if (newQuantity >= 0) {
            const newService = {...serviceDetail, quantity: newQuantity}
            setServiceDetail(newService);
            const url = `https://intense-tor-77999.herokuapp.com/item/${id}`;

            fetch(url, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(newService),
            })
                .then(response => response.json())
                .then(data => {
                    console.log('Success:', data);                
                })
                .catch((error) => {
                    console.error('Error:', error);
                }) 
        }
        else{
            toast(`${productName} is sold out`);
        }
    } 

But I just want to update only one property for example only quantity like

const updateQuantity = { quantity : newQuantity};

So, how can I update this one property without sending all properties from my frontend to backend?

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

0

Firstly you can declare const [isReload, setIsReload]= useState(true);

when you get response from database after uploading new quantity, add this

.then(response => response.json())
            .then(data => {
                console.log('Success:', data); 
                  setIsReload(!isReload);               
            });

than you put isReload as dependency of useEffect, where you load data by id

    useEffect(()=>{
       fetch(url)
        .then(res=>res.json())
      .then(data => setData(data))
    },[isReload ]);

try it

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!