I am building a React component but when rendering the component, the request done two times even if the first request success
import axios from 'axios';
import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router';
import Post from './Post';
export const SinglePost = () => {
const { id } = useParams();
const getData = async() => {
await axios.get(`post/${id}`)
.then((response)=>{
return response.data
})
.catch((err)=>{
return err
})
}
return <Post post = {getData()} />
}
Is there a common way to fix it? Thanks.
Here my index.tsx
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement);
root.render(
<Provider store ={store}>
<App />
</Provider>
);
The error happens I think when passing the result to Post
component. Even in that component, the request to that URL is never made.
Edit
I am rendering the component inside app.tsx
by this code
<Route path="post/:id" element={<SinglePost/>}/>
Edit2
Post object contains:
Object { id: 132, content: "", updated: "2022-10-08T09:56:37.070618Z", image: "http://127.0.0.1:8000/media/images/admin%40socialbook.org/posts/b2e5d26fff1965a4.jpeg", created: "2022-10-08T09:56:37.070618Z", author: {…} }