Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

fetch hook

import { useState, useEffect} from 'react'

function useFetchData(url) {
    const [data, setData] = useState(null);

    useEffect(() => {
      fetch(url)
        .then((res) => res.json())
        .then((data) => setData(data))
        .catch((err) => console.log(`Error: ${err}`));
    }, [url]);

    return { data };
}

export default useFetchData
Comment

using fetch hook

import useFetchData from './useFetchData'
 
function Users() {
    const { data } = useFetchData("https://api.github.com/users");

  return (
      <div>
          {data && (
            data.map((user) =>(
                <div className="text-white" key={user.id}>
                    <h1> {user.login} </h1>
                    <p> { user.type } </p>
                </div>
            ))
          )}
      </div>
  )
}

export default Users;
Comment

PREVIOUS NEXT
Code Example
Javascript :: string to number javascript shortcut 
Javascript :: javascript replace url on sentence as achor 
Javascript :: comment creer des switch en react js 
Javascript :: check if device is in dark mode js 
Javascript :: trigger many calls JS 
Javascript :: on scroll image blur jquery 
Javascript :: how to send email 
Javascript :: parentsuntil without jquery 
Javascript :: foreach doesnt return 
Javascript :: how to get author in wordpress api react 
Javascript :: Previously visited page with vanilla JavaScript 
Javascript :: botstrap 5 
Javascript :: javascript get element by class domlist undefined 
Javascript :: error 28 mongodb 
Javascript :: how to get the total price of all product in cart using react 
Javascript :: Private slots are new and can be created via Static initialisation blocks in classes 
Javascript :: koa wildcard route 
Javascript :: typeorm class validation 
Javascript :: react get padding 
Javascript :: how to identify the li anchor tag text is empty in javascript 
Javascript :: how to format date dd/mm/yyyy in javascript 
Javascript :: how to customize reactnative view dropshadow 
Javascript :: copy file using java script 
Javascript :: javascript coding test interview 
Javascript :: regular expression for twitter embedded tweets 
Javascript :: how is react different from normal js 
Javascript :: jsx tag with children react js 
Javascript :: JS check the type stored in the name variable in JS 
Javascript :: react native charts style gradiant 
Javascript :: angular view not changing on model 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =