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

How to get data from an API only once when the page is loaded using axios.get method?

I am a beginner in React, I want to get the data from my API only once when the particular page is loaded and save it to a variable i.e. "todo". I can see in my backend code that this code is sending continuous request to the api.

const [todo, setTodo] = useState({})
        const fetchdata = async () => {
            const res = await axios.get(`http://localhost:8000/api/get-todo/${nid}`);
            setTodo(res.data);
        };
        useEffect(() => {
            fetchdata();
        },)

>Solution :

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

You need to use square brackets ([]) at the end of the useEffect hook to make the send request one time.

useEffect(() => {
    fetchdata();
}, [])
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