简体   繁体   中英

console.log() statement being called twice in Reactjs

My code is as follows:

//fetch tasks from server
      const fetchTasks=()=>{
    
        const fTask = async ()=>{
          const res = await fetch('http://localhost:5000/tasks')
          const data = await res.json()
    
          console.log("data", data)     
        }
    
        fTask()
    
      }
    
      fetchTasks()

I was expecting the data to be logged into the console only once, but for some reason, it is being logged twice. I tried debugger, but it too did not help.. thank you in advance..

  1. Remove strict mode or turn it into <> </> fragments shortcut

do this instead

const fetchTasks= async ()=>{
      const res = await fetch('http://localhost:5000/tasks')
      const data = res.json()

      console.log("data", data)  
  }

  fetchTasks()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM