Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API getting called twice in React [duplicate]

My API is getting called twice on the browser page reload as checked in the console. Can you please suggest? I am using Axios to call the API in React.

import React, {useState,useEffect} from "react"

import {Container,Row,Col} from "reactstrap";
import "bootstrap/dist/css/bootstrap.min.css"
import './App.css';

import Axios from "axios";
import MyCard from "./MyCard";

function App() {

  const[details,setDetails]=useState({});

  const fetchDetails=async ()=>{
    const {data}=await Axios.get("https://randomuser.me/api/");
    console.log("RESPONSE:",data);
    const details=data.results[0];
    setDetails(details)
  }

  useEffect(()=>{
    fetchDetails();
  },[])

  return (
    <Container fluid className="p-4 bg-primary App">
      <Row>
        <Col md={4} className="offset-md-4 mt-4">
          <MyCard details={details}/>
        </Col>
      </Row>
    </Container>
  );
}

export default App;
like image 615
Srijan Avatar asked Apr 12 '26 15:04

Srijan


1 Answers

Fixed 1

This only happens in dev mode. Going live will solve your problem.

Fixed 2

Removing strict mode will solve your problem.

For Example

root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

To

root.render(
    <App />
);

like image 73
Maxwell D. Dorliea Avatar answered Apr 15 '26 05:04

Maxwell D. Dorliea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!