Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios get request for real-time data

For simplicity assume that I have entity user and in my frontend side (react) I am making an Axios get request to get all users to display them.

In the componentDidMount I am doing a get request const res = axios.get(myurl) then map the res to be displayed.

However, if a new user is entered in the database I have to refresh the page to see the update.

Here is my question, how to make the get request to be like running or something like this so if any changes happens in the database it reflects the get request.

like image 490
Yahia Badr Avatar asked Nov 16 '22 05:11

Yahia Badr


1 Answers

You can use the useEffect in react

import React,{useEffect}from 'react'

function getAllUsers(){
    //your code here...
}

useEffect(()=>{
        getAllUsers();

    })
like image 165
Mat Mitcheil Ando Avatar answered Dec 15 '22 15:12

Mat Mitcheil Ando