Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't get Axios post information

For my posts

in component AboutUsers.jsx

  const [users, setUsers] = useState([]);

if I write like this, it’s working, I see posts in users:

in component AboutUsers.jsx

 useEffect(()=> {
        const getUsers = axios.get('https://jsonplaceholder.typicode.com/todos',{
            params:{
                _limit:limitPage,
                _page:currentPage
            }
        })
            .then(response => setUsers(response.data))
    },[])

but I created other component PostMyServise.js with:

export default class PostMyServise {

    static async getPost(limit=10, page=1) {
        const result  = await axios.get('https://jsonplaceholder.typicode.com/todos',{
            params: {
                _limit: limit,
                _page: page,
            }
    })
            .then(response => {
                return response
            })
        return result;
    }
}

And one yet component useCreatePosts.js:

export const usePosts = (callback) => {
   const [isTrue, setIsTrue] = useState('')
   const [error, setError] = useState('')


   const createPost = async () => {
       try {
           setIsTrue(false);
           await callback;
       } catch (e) {
               setError(e.message);
       } finally {
               setIsTrue(true);
       }
   }
return [createPost, isTrue, error];
}

export default usePosts;

I wrote this, and I see empty array in console.log(users):

I don’t understand why array is empty

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  const [createPost, isTrue, error] = usePosts (async ()=> {
        const response = await PostMyServise.getPost(limitPage, currentPage);
        setUsers(response.data)
    })

 useEffect(() => {
            createPost();
        },[currentPage])

>Solution :

You are not calling the callback. You need to add the parentheses.

   const createPost = async () => {
       try {
           setIsTrue(false);
           await callback();
       } catch (e) {
               setError(e.message);
       } finally {
               setIsTrue(true);
       }
   }

I feel like something about your code is over-engineered and too complex but that’s outside the scope of the question. This change will at least get it working. Also, I suggest changing the name of isTrue and setIsTrue to something more meaningful as those names do not tell you what they are for.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading