Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react : calling APIs after render w error message

import { useState, useEffect } from 'react';
import axios from 'axios';

function App() {
  const [imageUrl, setImageUrl] = useState('https://images.dog.ceo/breeds/hound-afghan/n02088094_1003.jpg');
  const [errorMessage, setErrorMessage] = useState('');

  useEffect(() => {
    axios.get('https://dog.ceo/api/breeds/image/random')
      .then((response) => {
        setImageUrl(response.data.message);
      })
      .catch((error) => {
        setErrorMessage(<section>{error.response.data.message}</section>);
      });
  }, []);

  return (
    <div>
      <h1>My Dog Log</h1>
      {errorMessage}
      <div>
        <img src={imageUrl} alt="A random dog" />
      </div>
    </div>
  );
}
Comment

react creating function to call API in app: calling APIs after render w error message

import { useState } from 'react';

function App() {
  const [imageUrl, setImageUrl] = useState('https://images.dog.ceo/breeds/hound-afghan/n02088094_1003.jpg');

  return (
    <div>
      <h1>My Dog Log</h1>
      <div>
        <button onClick={() => { console.log("The button was clicked!"); }}>Get New Random Dog Image</button>
        <img src={imageUrl} alt="A random dog" />
      </div>
    </div>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: JavaScript is synchronous by default 
Javascript :: how to validate date in react js 
Javascript :: top of stack javascript 
Javascript :: discord.js create a private channel 
Javascript :: toISOString() in electron 
Javascript :: Proper Way To Access Model(s) Data From Collection In Backbone 
Javascript :: How many options are there to climb a ladder with N 
Javascript :: document.getelementbyid add number 
Javascript :: javascript for backend 
Javascript :: js check that interactive element is not focused 
Javascript :: how to convert python code to javascript 
Javascript :: responsive navbar react 
Javascript :: remove decimal places js 
Javascript :: convert jquery to javascript converter online tool 
Javascript :: dom traversal jquery 
Javascript :: find an element 
Javascript :: implement the nationalize api using async/await with fetch. 
Javascript :: Remove uploaded file in jquery 
Javascript :: animation js 
Javascript :: url enocde in javascript 
Javascript :: js how to get n fibonacci number 
Javascript :: JavaScript Rules for Naming JavaScript Variables 
Javascript :: javascript + Operator with Numbers 
Javascript :: javascript get() handler 
Javascript :: JavaScript HTML DOM Navigation 
Javascript :: what does this operation tell if(!arr.some(isNaN)) JavaScript 
Javascript :: Could not resolve "i18n-iso-countries" 
Javascript :: Json to npy file 
Javascript :: phaser animation on complete event 
Javascript :: permissions in chrome extension javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =