• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

129
Views
How to update state of parent component after the server is updated in child component?

I have a todo list and after adding an item to it in the child component the change is not reflected straight away. I have to manually reload the page to see the change.The structure is something like this.

ToDo(Parent Componenet)
  -> Todoi(Child Component)

I am updating data in the child component but as I said earlier it is not reflecting the change unless I reload the page manually. If I can somehow change the state in parent component through child component then the component will reload itself but I am not able to do it. Here is the part where I am updating the list.

const handleList= (e) => {
      e.preventDefault();
      console.log(e);
      console.log(addtodo);
      axios.post('/list',{
        data:addtodo,
        userid:username
      }).then(function(res){
        if(res.data === "ToDo Updated"){
          navigate(`/todo/${username}`);
        }
      });
    }

What should I do to make it?

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

0

You can either pass the state setter to your child component and then add the new entry in your post function:

const handleList= (e) => {
      e.preventDefault();
      console.log(e);
      console.log(addtodo);
      axios.post('/list',{
        data:addtodo,
        userid:username
      }).then(function(res){
        if(res.data === "ToDo Updated"){
          setList(prevState => prevState.concat(addtodo));
        }
      });
    }
<YourComponent setList={setList}/>

Or you can add a callback function prop to your component which is executed on successful request like so:

const handleList= (e) => {
      e.preventDefault();
      console.log(e);
      console.log(addtodo);
      axios.post('/list',{
        data:addtodo,
        userid:username
      }).then(function(res){
        if(res.data === "ToDo Updated"){
          onSuccess(addtodo)
        }
      });
    }
<YourComponent onSuccess={(todo)=>setList(prevState => prevState.concat(todo))}/>

I'm not sure what your state/data type looks like but I hope this helps!

about 3 years ago · Juan Pablo Isaza Report

0

Usually, a react component will be rerendered under two situations:

  1. The states have been updated.
  2. The props have been updated.

Therefore, we could manually trigger the status change of the parent component in the child component or update the server response as the props of the parent component.

I don't know how you handle your data in the parent component, but I guess you might need to make React detect your data updating.

about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error