FNER
FNER

Reputation: 29

React - How to update my component when there is a change on the server

I want to reload the page when a put request is successful but it never reloads the page. what is the problem

async function saveEdit() {
        await Axios.put('http://localhost:5000/edit', {id: id, newTitle: title, newDescription: description, newImageURL: imageURL});
        window.location.reload();
       
        
    }

the request works but the reload() line doesn't seem to work. or is there a better way to do this?

Upvotes: 1

Views: 1054

Answers (1)

JaivBhup
JaivBhup

Reputation: 855

A simple implementation of useState() hook in react

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [data, setData] = useState(null);
  async function saveEdit(id, title, description, imageURL) {
        // Make sure that your request responds back with the relevant and fresh batch of data after the update
        var res = await Axios.put('http://localhost:5000/edit', {id: id, newTitle: title, newDescription: description, newImageURL: imageURL});
        //window.location.href = window.location.href;
        var data = res.data;
        setData(data)
    }
  return (
    <div>
       {data?.map(d=>{
           // How you want to processes your data object.
           return <h1>d.title</h1>
        })}
        <button onClick={()=>saveEdit(1,"Mytitle","description","test Url")}> Update </button>
    </div>
  );
}

For more information in react hooks refer here.

Upvotes: 1

Related Questions