Skip to content

Instantly share code, notes, and snippets.

@dnh33
Last active November 10, 2021 13:51
Show Gist options
  • Save dnh33/f2ff9df85dcf2d70916691dcdb973b1f to your computer and use it in GitHub Desktop.
Save dnh33/f2ff9df85dcf2d70916691dcdb973b1f to your computer and use it in GitHub Desktop.
React Fetch Request with State
const [stuff, setStuff] = useState([]);
const request = "example/tasks.json", {
        method: "POST",
        body: JSON.stringify(taskData),
        headers: { "Content-Type": "application/json" },
      };
    
useEffect( ()=> {
    fetch(request)
    .then(response => response.json())
    .then(data => setStuff(data.hits))
    .catch(err => {console.log(err)})
},[])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment