陆迎 發表於 2020-4-3 03:06:00

[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&nbsp;SWR&nbsp;- 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>&nbsp;</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) =&gt;<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 =&gt; 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>&nbsp;</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) =&gt;<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>= () =&gt;<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> &lt;Alert status="error"&gt;Loading fail&lt;/Alert&gt;;
<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> &lt;Alert status="info"&gt;Loading...&lt;/Alert&gt;;
<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>&lt;SimpleGrid columns={2} width="2xs" spacingY={4}&gt;
      &lt;Text fontWeight="bold" marginRight={4}&gt;<span style="color: rgba(0, 0, 0, 1)">
      UserID
      </span>&lt;/Text&gt;
      &lt;Text&gt;{data.id}&lt;/Text&gt;

      &lt;Text fontWeight="bold" marginRight={4}&gt;<span style="color: rgba(0, 0, 0, 1)">
      Name
      </span>&lt;/Text&gt;
      &lt;Text&gt;{data.name}&lt;/Text&gt;

      &lt;Text fontWeight="bold" marginRight={4}&gt;<span style="color: rgba(0, 0, 0, 1)">
      Email
      </span>&lt;/Text&gt;
      &lt;Text&gt;{data.email}&lt;/Text&gt;
    &lt;/SimpleGrid&gt;
<span style="color: rgba(0, 0, 0, 1)">);
};

const user </span>= () =&gt;<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>&lt;Flex flexDirection="column" alignItems="center"&gt;
      &lt;Heading as="h1"&gt;User&lt;/Heading&gt;
      &lt;UserData /&gt;
    &lt;/Flex&gt;
<span style="color: rgba(0, 0, 0, 1)">);
};

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> user;</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/Answer1215/p/12624213.html
頁: [1]
查看完整版本: [Next.js] Consume Next.js API routes with the SWR library on the client-side