Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

using await and axios in react

import axios from "axios";
import React, { useEffect, useState } from "react";

export default function App() {
  const [val, setVal] = useState();

  const getAnswer = async () => {
    const { data } = await axios("https://yesno.wtf/api");
    setVal(data.answer);
  };

  useEffect(() => {
    getAnswer();
  }, []);

  return <div>{val}</div>;
}
Comment

async wait for axios reactjs

useEffect(() => {
  async function fetchData() {
    // You can await here
    const response = await MyAPI.getData(someId);
    // ...
  }
  fetchData();
}, [someId]); // Or [] if effect doesn't need props or state
Comment

PREVIOUS NEXT
Code Example
Javascript :: uppercase first letter javascript 
Javascript :: javascript sort a b 
Javascript :: check if variable is set javascript 
Javascript :: convert json into map in java example 
Javascript :: express octet stream 
Javascript :: reactjs firebase timestamp to date 
Javascript :: make react project 
Javascript :: html content in jspdf 
Javascript :: javascript map 
Javascript :: backtick string javascript 
Javascript :: nodejs delete in mysql 
Javascript :: 7) Change cursor:pointer at checkboxes in java script 
Javascript :: javascript .firstordefault 
Javascript :: uuid in node js 
Javascript :: jquery json to table 
Javascript :: 404 page in react 
Javascript :: javascript get next 15min 
Javascript :: javascript how to get last property of object 
Javascript :: how-to-reset-a-form-using-jquery 
Javascript :: how to use graphql api in react 
Javascript :: static js 
Javascript :: see all set variables chrome 
Javascript :: bootstrap 4 modal popup remote url 
Javascript :: two digits number javascript 
Javascript :: get key for value javascript 
Javascript :: javascript convert input to lowercase 
Javascript :: select the items from selectors in .map reactjs 
Javascript :: get milliseconds since epoch for 12am today javascript 
Javascript :: Minimal Project Angular 
Javascript :: react-router useNavigate 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =