UseState – Invalid hook call. Hooks can only be called inside the body of a function component

  • I am using useState to update response from API.

  • Then I am taking the data I need, and map them in a new array (arr)

  • After the above step I am updating the “setData” useState hook to use “data” state

  • But it is giving me Error
    “Invalid hook call. Hooks can only be called
    inside the body of a function component “

Here is the part of the code i am facing problem with

import {useEffect, useState} from 'react'
import logo from './logo.svg';
import './App.css';
import axios from 'axios'
import ReactSearchBox from 'react-search-box'
import { set } from 'express/lib/application';

function App() {

 
  let [data, setData] = useState()
  
  useEffect(onMount,[])

  function onMount(){
      
      axios.get('https://restcountries.com/v3.1/all').then(res=>{
        
        let arr = res.data.map((c)=>{
          return {key:c.name.common, value: c.name.common}
        })
        setData(arr)   
      })

     
  }

My Full Page Code

import "./styles.css";
import axios from 'axios'
import ReactSearchBox from 'react-search-box'
import {useEffect, useState} from 'react'

export default function App() {

  let [data, setData] = useState()
  
  useEffect(onMount,[])

  function onMount(){
      
      axios.get('https://restcountries.com/v3.1/all').then(res=>{
        
        let arr = res.data.map((c)=>{
          return {key:c.name.common, value: c.name.common}
        })
        setData(arr)
      })

     
  }
  return (
    <div className="App">
      <h1>Countries</h1>
      {data.length>0 ? <ReactSearchBox
        placeholder="Placeholder"
        value="Doe"
        data={data}
        callback={(record) => console.log(record)}
      />: null}
    </div>
  );
}

Picture of Error

enter image description here