Fetch with useEffect
The simplest pattern for React prototypes — fetch on mount and store in state.
import { useEffect, useState } from "react";
const API_URL = "https://free-api-backend.vercel.app/users?count=10";
export function UserList() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch(API_URL)
.then((res) => res.json())
.then((json) => setUsers(json.data ?? []));
}, []);
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.firstName} {user.lastName}</li>
))}
</ul>
);
}React Query
Use ApiGenerator URLs as queryFn targets for caching and loading states.
import { useQuery } from "@tanstack/react-query";
function useMockUsers() {
return useQuery({
queryKey: ["mock-users"],
queryFn: async () => {
const res = await fetch("https://free-api-backend.vercel.app/users?count=20&fields=id,firstName,email");
const json = await res.json();
return json.data;
},
});
}Frequently asked questions
How do I mock an API in React without a backend?
Point your fetch or axios calls to an ApiGenerator endpoint like /users or /products. The API returns realistic JSON arrays you can use directly in state or with data-fetching libraries.
Can I use React Query with ApiGenerator?
Yes. ApiGenerator endpoints are standard REST URLs. Use them as queryFn targets in React Query, SWR, or any HTTP client.
Does mock data stay consistent between requests?
Use the seed query parameter to get deterministic data across list and detail requests, which is useful for testing navigation flows.