Advertisement
Falak_Ahmed_Shakib

data load fetch useEffect, useState

May 8th, 2022 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import logo from './logo.svg';
  2. import './App.css';
  3. import { useEffect, useState } from 'react'; // useEffect, useState need
  4.  
  5. function App() {
  6. // data load kore ek state a rakte hobe [] mane potom empty array takbe
  7. const [countries, setCountries] = useState([]);
  8. // useEffect (2ta pera 1. function fetch korbo , 2. ek bar load ar jonno [])
  9. useEffect(() => {
  10. fetch('https://restcountries.com/v3.1/all')
  11. .then(res => res.json())
  12. .then(data => setCountries(data)) // (ekane data load ar por kit korbe console kore dekte pare otoba set korte paro)
  13. .catch(error => console.log(error)) // error find
  14. }, [])
  15.  
  16.  
  17. return (
  18. <div className="App">
  19. <h1>countries numbers : {countries.length}</h1>
  20.  
  21.  
  22. <header className="App-header">
  23. <img src={logo} className="App-logo" alt="logo" />
  24. <p>
  25. Edit <code>src/App.js</code> and save to reload.
  26. </p>
  27. <a
  28. className="App-link"
  29. href="https://reactjs.org"
  30. target="_blank"
  31. rel="noopener noreferrer"
  32. >
  33. Learn React
  34. </a>
  35. </header>
  36. </div>
  37. );
  38. }
  39.  
  40. export default App;
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement