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 :: Node-Red: Bit Switch 
Javascript :: create sub array from array with values that pass condition javascript 
Javascript :: linux pupperteer 
Javascript :: compile regex script help online 
Javascript :: call url many times 
Javascript :: Simple Email Validation, Case Insensitive, w/ All Valid Local Part Characters (whatever tf that means to you...), 2nd Example - Regex 
Javascript :: string format javascript 
Javascript :: js react change slide by touch event 
Javascript :: replace for ifelse 
Javascript :: downlaod file from website raect2 
Javascript :: add types to React$Context in flow 
Javascript :: Make an array from the HTML Collection to make it iterable 
Javascript :: c program to print triangle using recursion in javascript 
Javascript :: js decrypt online 
Javascript :: javascript activate file input 
Javascript :: Private slots are new and can be created via Private methods and accessors 
Javascript :: Focus next input once reaching maxlength value 
Javascript :: typeorm class validator 
Javascript :: react stream chat 
Javascript :: reverse not working react 
Javascript :: SuiteScript https.post a pdf file 
Javascript :: Without a custom hook example in react 
Javascript :: js hide modal event listener name 
Javascript :: let a local variable 
Javascript :: ReactComponent as DeleteIcon 
Javascript :: tailwind intenseness react 
Javascript :: make a circle in javascript 
Javascript :: normalizedList.flatMap is not a function vue draggable 
Javascript :: javascript error fix 
Javascript :: provider not found react 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =