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 :: recursive function and json object 
Javascript :: swal on submit angular with cancel butotn 
Javascript :: Backbone With Express 
Javascript :: discord.js sync channel permissions 
Javascript :: dockerignore node_modules 
Javascript :: Backbone Sync And Fetch 
Javascript :: file path to blob javascript 
Javascript :: Backbone View Notes 
Javascript :: Wrong Model Name For Backbone: Code Still Runs 
Javascript :: unknown set of argument 
Javascript :: get user badge discordjs 
Javascript :: javascript asynchronous function list 
Javascript :: cookies javascript 
Javascript :: use of prototype in javascript 
Javascript :: router.push 
Javascript :: javascript find vs filter 
Javascript :: call bind apply in javascript 
Javascript :: static in class javascript 
Javascript :: animate js 
Javascript :: how to add prefix to a string in javascript 
Javascript :: vscode module path aliases 
Javascript :: js for of loop 
Javascript :: JavaScript Precision Problems 
Javascript :: javascript Read Only View of an Object 
Javascript :: JavaScript HTML DOM Collections 
Javascript :: what is hmr in console 
Javascript :: javascipt 
Javascript :: node add dependency 
Javascript :: phaser animation on repeat event 
Javascript :: Six escape sequences are valid in JavaScript 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =