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

115
Views
How to get data in useEffect hook?

I am new to react and trying to fetch data using axios but its not working, can I use async/await syntax in the useEffect hook?

./api/index.js

import axios from 'axios'

const url = 'http://localhost:5000/posts'

export const getPosts = async ()=>{
    const data = await axios.get(url).data
    return data
}

export const createPost = async (data)=>{
    const result = await (await axios.post(url, data)).data
    return result
}

Post.js

import React, { useEffect, useState } from 'react'
import { getPosts } from '../api'
export const Post = () => {
    const [post, setPost] = useState([])
    useEffect(()=>{
        setPost(getPosts().then(data=>data))
        console.log(post)
    }, [])

  return (
    <>
    POSTS
    </>
  )
}

export default Post

App.js

import React from 'react'
import { useEffect } from 'react'
import { getPosts } from './api'
import Form from './components/Form'
import Post from './components/Post'
const App = () => {
  
    useEffect(()=>{
        const data =  getPosts()
        console.log(data)
    })
  
    return (
        <>
        <Form/>
        <Post/>
        <div>App</div>
        </>
  )
}

export default App

I want to console.log the data as well as use the data in the setPost hook to change the value of the post

about 4 years ago · Juan Pablo Isaza
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!