Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

using hooks with apis

import React, { useEffect, useState } from "react";
import styled from "styled-components";

const App = () => {
    const [advice, setAdvice] = useState("");

    useEffect(() => {
        const url = "https://api.adviceslip.com/advice";

        const fetchData = async () => {
            try {
                const response = await fetch(url);
                const json = await response.json();
                console.log(json.slip.advice);
                setAdvice(json.slip.advice);
            } catch (error) {
                console.log("error", error);
            }
        };

        fetchData();
    }, []);

    return (
        <Wrapper>
            <Paragraph>{advice}</Paragraph>
        </Wrapper>
    );
};

export default App;

const Wrapper = styled.div`
    padding-top: 150px;
    margin: 0 auto;
`;

const Paragraph = styled.h2`
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
    font-style: normal;
    font-weight: bold;
    font-size: 20px;
    line-height: 48px;
    text-align: center;
`;
Comment

PREVIOUS NEXT
Code Example
Javascript :: Scaling an image to fit on canvas 
Javascript :: get image src width and height 
Javascript :: switch react router 
Javascript :: prevent onclick event javascript 
Javascript :: Run project in visual studio with iis express 
Javascript :: nested dto nestjs 
Javascript :: node 10 form data 
Javascript :: prevent click on pseudo element javascript 
Javascript :: js object sort 
Javascript :: js check if a string is a number 
Javascript :: javascript get params from query string json object 
Javascript :: react useEffect prevent first time 
Javascript :: mongoose remove data 
Javascript :: jquery fixed element on scroll footer 
Javascript :: why array.map returns undefined 
Javascript :: javascript iterate over map values 
Javascript :: how to get the last two characters of a string in javascript 
Javascript :: javascript check if array is empty or null or undefined 
Javascript :: does pycharm support javascript 
Javascript :: react-native-restart 
Javascript :: what is a promise 
Javascript :: import and export type in js 
Javascript :: classlist.contain in javascript 
Javascript :: using python with javascript 
Javascript :: react context api 
Javascript :: javascript addeventlistener button 
Javascript :: decimal to base 32 javascript 
Javascript :: json vs xml 
Javascript :: how sum all array element with for 
Javascript :: json comment 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =