Yahia Naguib
Yahia Naguib

Reputation: 311

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.

Upvotes: 5

Views: 3929

Answers (2)

Yahia Naguib
Yahia Naguib

Reputation: 311

This is a real-time updates, and we have multiple practices for such a case.

Before reading the answer, you need to know that this case is about the real-time updates concept and not a problem with a line of code solution. You need to implement real-time system in your app to achieve what you want.

1st Practice

  • Is to use a real-time library like Socket.io, so that you can listen to the events you want. You can find here an example for Node.JS with React.JS

2nd Practice

  • Is to use a database that provides real-time updates like Firebase. Here you can find an example.

Upvotes: 1

Mat Mitcheil Ando
Mat Mitcheil Ando

Reputation: 11

You can use the useEffect in react

import React,{useEffect}from 'react'

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

useEffect(()=>{
        getAllUsers();

    })

Upvotes: 1

Related Questions