Advertisement
WillmanCodes

Untitled

Oct 20th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useEffect, useState} from 'react';
  2. import ReactDOM from 'react-dom'
  3.  
  4. const SomeComponent = () => {
  5.     const [id, setId] = useState(0);
  6.     const [data, setData] = useState({currencies:[], isFetching:false});
  7.  
  8.     const images = [
  9.         "https://images.pexels.com/photos/672532/pexels-photo-672532.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
  10.         "https://images.pexels.com/photos/773471/pexels-photo-773471.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
  11.         "https://images.pexels.com/photos/64271/queen-of-liberty-statue-of-liberty-new-york-liberty-statue-64271.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
  12.     ];
  13.  
  14.     useEffect(()=> {
  15.         const getCurrentCurrency = async () => {
  16.             try{
  17.                 setData({currencies: data.currencies, isFetching: true});
  18.                 const currencyArr = [];
  19.                 const response = await fetch(`https://api.exchangeratesapi.io/latest?base=GBP`);
  20.                 const responseData  = await response.json();
  21.                 const {EUR:euro ,CHF:franc, USD: dolar} = responseData.rates;
  22.                 currencyArr.push(euro,franc,dolar);
  23.                 setData({currencies: currencyArr, isFetching: false});
  24.             }
  25.             catch (e) {
  26.                 setData({currencies: data.currencies, isFetching: false});
  27.             }
  28.         };
  29.         getCurrentCurrency();
  30.     }, [id]);
  31.  
  32.  
  33.  
  34.     const goToPrevSlide = () => {
  35.          id === 0 ? setId(2) : setId(id-1);
  36.     }
  37.     const goToNextSlide = () =>{
  38.          id === 2 ? setId(0) : setId(id+1);
  39.     }
  40.  
  41.  
  42.     return(
  43.         <div>
  44.             <div className="slide">
  45.                 <div>
  46.                     {id}
  47.                 </div>
  48.                 {JSON.stringify(data)}
  49.                 <div>
  50.                     <button onClick={goToPrevSlide}>Prev</button>
  51.                     <button onClick={goToNextSlide}>Next</button>  
  52.                 </div>
  53.             </div>
  54.         </div>
  55.     );
  56. }
  57.  
  58. ReactDOM.render(
  59.   <SomeComponent />,
  60.   document.getElementById("react")
  61. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement