I have an error when I call an API, I think this error is failed to compile, because this error indicates to me:
Cannot read property 'type' of undefined in line 18
I run fetchCoins in useEffect, how to I can solve this error?
I dont know why fetchCoins is undefined!?
import './App.css';
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import Coin from './Coin';
import Pagination from 'react-js-pagination'
const App = () => {
const [coins,setCoins] = useState([]);
const [search , setSearch] = useState('');
const [loading, setLoading] = useState(false);
const [currentPage, setCurrentPage] = useState(1);
const [postsPerPage] = useState(10);
useEffect(() => {
const fetchCoins = () => {
setLoading(true);
axios.get('https://api............')
.then(res => { setCoins(res.data)})
.catch(err => console.log(err))
setLoading(false);
};
fetchCoins()
}, [])
const handleChange = e => {
setSearch(e.target.value)
}
const filteredCoins = coins.filter(coin =>
coin.name.toLowerCase().includes(search.toLowerCase())
);
console.log(fetchCoins())
return (
<div className="coin-app">
<div className="coin-search">
<form>
<input type="text" placeholder="search" className="coin-input" onChange={handleChange} />
</form>
</div>
{filteredCoins.map(coin => {
return (
<Coin
key={coin.id}
name={coin.name}
price={coin.current_price}
symbol={coin.symbol}
marketcap={coin.total_volume}
volume={coin.market_cap}
image={coin.image}
priceChange={coin.price_change_percentage_24h}
/>
);
})}
</div>
);
}
export default App;
console.log(fetchCoins())outside youruseEffectbecause you usedconstwhich is block scoped. Read more here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…