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

133
Views
Render page when database contents changes

So suppose I have a page that displays all the user's posts. Right now when the user creates a post, it stores it into the database. But that new post will not appear unless I log out of the app and log back in. How can I make it so that the post will appear without having to logout?

const [Posts,setPosts] = useState([])

useEffect(()=>{
const fetchUserPosts = async () => {
const res = await client.get('/fetch-user-posts')
setPosts(res.data.posts)
}
fetchUserPosts()
},[])

And in the rendering I just took the Posts and map it to display its contents. I know this problem exists somewhere but I don't know what keywords to google. I have seen posts asking to use JQuery but I bet there is a simpler solution? Any pointers are appreciated.

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

0

Take the function of fetchUserPosts out of the useEffect like this:

    const fetchUserPosts = async () => {
        const res = await client.get('/fetch-user-posts')
        setPosts(res.data.posts)
    }

Now, useEffect will be like this:

    useEffect(() => {
        fetchUserPosts()
    }, [])

Next, wherever you function of create user posts is, call fetchUserPosts() like this:

    const addUserPosts = async () => {
        // logic of adding new user post
        if (success) {
            await fetchUserPosts()
        }
    }
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!