[Next.js] Consume Next.js API routes with the SWR library on the client-side
<p>The cool thing about Next.js API routes is that we can directly consume the API endpoints from pages.</p><p>SWR is a nice tool for handling data loading state in React apps using hooks, and is a perfect companion for our Next.js application. In this lesson, we will learn how to use SWR - a data fetching library by Zeit - to consume API endpoints on the client-side, and conditionally render an error or a loading component when the data is not available.</p>
<p> </p>
<p>pages/api/user/.ts:</p>
<div class="cnblogs_code">
<pre>import { NextApiHandler } from "next"<span style="color: rgba(0, 0, 0, 1)">;
const data </span>=<span style="color: rgba(0, 0, 0, 1)"> [
{ id: </span>1, name: "Wan"<span style="color: rgba(0, 0, 0, 1)"> },
{ id: </span>2, name: "Zhen"<span style="color: rgba(0, 0, 0, 1)"> },
{ id: </span>3, name: "Tian"<span style="color: rgba(0, 0, 0, 1)"> }
];
const user: NextApiHandler </span>= (req, res) =><span style="color: rgba(0, 0, 0, 1)"> {
const { id } </span>=<span style="color: rgba(0, 0, 0, 1)"> req.query;
const userData </span>= data.find(x => String(x.id) ===<span style="color: rgba(0, 0, 0, 1)"> String(id));
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (userData) {
res.status(</span>200<span style="color: rgba(0, 0, 0, 1)">).json(userData);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
res.status(</span>404<span style="color: rgba(0, 0, 0, 1)">).end();
}
};
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> user;</pre>
</div>
<p> </p>
<p>pages/user/.tsx:</p>
<div class="cnblogs_code">
<pre>import { useRouter } from "next/router"<span style="color: rgba(0, 0, 0, 1)">;
import { SimpleGrid, Text, Alert, Flex, Heading } from </span>"@chakra-ui/core"<span style="color: rgba(0, 0, 0, 1)">;
import useSWR from </span>"swr"<span style="color: rgba(0, 0, 0, 1)">;
const fetcher </span>= async (url: string) =><span style="color: rgba(0, 0, 0, 1)"> {
const res </span>=<span style="color: rgba(0, 0, 0, 1)"> await fetch(url);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">res.ok) {
</span><span style="color: rgba(0, 0, 255, 1)">throw</span> Error("Yo that's NOT OK!!!"<span style="color: rgba(0, 0, 0, 1)">);
}
const data: {
id: string;
name: string;
email: string;
} </span>=<span style="color: rgba(0, 0, 0, 1)"> await res.json();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> data;
};
const UserData </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
const router </span>=<span style="color: rgba(0, 0, 0, 1)"> useRouter();
const { id } </span>=<span style="color: rgba(0, 0, 0, 1)"> router.query;
const { data, error } </span>= useSWR(`/api/user/${id}`, fetcher);
<span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (error) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <Alert status="error">Loading fail</Alert>;
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">data) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <Alert status="info">Loading...</Alert>;
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><SimpleGrid columns={2} width="2xs" spacingY={4}>
<Text fontWeight="bold" marginRight={4}><span style="color: rgba(0, 0, 0, 1)">
UserID
</span></Text>
<Text>{data.id}</Text>
<Text fontWeight="bold" marginRight={4}><span style="color: rgba(0, 0, 0, 1)">
Name
</span></Text>
<Text>{data.name}</Text>
<Text fontWeight="bold" marginRight={4}><span style="color: rgba(0, 0, 0, 1)">
</span></Text>
<Text>{data.email}</Text>
</SimpleGrid>
<span style="color: rgba(0, 0, 0, 1)">);
};
const user </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><Flex flexDirection="column" alignItems="center">
<Heading as="h1">User</Heading>
<UserData />
</Flex>
<span style="color: rgba(0, 0, 0, 1)">);
};
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> user;</pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/Answer1215/p/12624213.html
頁:
[1]