Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, {useEffect, useState} from 'react';
- import ReactDOM from 'react-dom'
- const SomeComponent = () => {
- const [id, setId] = useState(0);
- const [data, setData] = useState({currencies:[], isFetching:false});
- const images = [
- "https://images.pexels.com/photos/672532/pexels-photo-672532.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
- "https://images.pexels.com/photos/773471/pexels-photo-773471.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
- "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"
- ];
- useEffect(()=> {
- const getCurrentCurrency = async () => {
- try{
- setData({currencies: data.currencies, isFetching: true});
- const currencyArr = [];
- const response = await fetch(`https://api.exchangeratesapi.io/latest?base=GBP`);
- const responseData = await response.json();
- const {EUR:euro ,CHF:franc, USD: dolar} = responseData.rates;
- currencyArr.push(euro,franc,dolar);
- setData({currencies: currencyArr, isFetching: false});
- }
- catch (e) {
- setData({currencies: data.currencies, isFetching: false});
- }
- };
- getCurrentCurrency();
- }, [id]);
- const goToPrevSlide = () => {
- id === 0 ? setId(2) : setId(id-1);
- }
- const goToNextSlide = () =>{
- id === 2 ? setId(0) : setId(id+1);
- }
- return(
- <div>
- <div className="slide">
- <div>
- {id}
- </div>
- {JSON.stringify(data)}
- <div>
- <button onClick={goToPrevSlide}>Prev</button>
- <button onClick={goToNextSlide}>Next</button>
- </div>
- </div>
- </div>
- );
- }
- ReactDOM.render(
- <SomeComponent />,
- document.getElementById("react")
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement