Skip to content

Instantly share code, notes, and snippets.

@onuro
Last active May 16, 2023 09:22
Show Gist options
  • Save onuro/e596fbde4eabfc15408cef3c97bb5893 to your computer and use it in GitHub Desktop.
Save onuro/e596fbde4eabfc15408cef3c97bb5893 to your computer and use it in GitHub Desktop.
React Fetch JSON and return markup value example
// Welcome to Code in Framer
// Get Started: https://www.framer.com/docs/guides/
import React, { useEffect, useState } from "react"
const Card = (props) => {
const { user } = props
return (
<div>
<div style={{ color: "red" }}>
{user.name.title} {user.name.first} {user.name.last}
</div>
<hr />
</div>
)
}
export default function DNetwork() {
const [users, setUsers] = useState([])
useEffect(() => {
const url = "https://randomuser.me/api/?results=5"
const fetchData = async () => {
try {
const response = await fetch(url)
const json = await response.json()
const { results } = json
// Only put the results in state, ie, the actual users array
setUsers(results)
} catch (error) {
console.log("error", error)
}
}
fetchData()
}, [])
return (
<div>
{users.map((user) => (
<Card key={user.email} user={user} />
))}
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment