zoukankan      html  css  js  c++  java
  • [Next.js] Consume Next.js API routes with the SWR library on the client-side

    The cool thing about Next.js API routes is that we can directly consume the API endpoints from pages.

    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.

    pages/api/user/[id].ts:

    import { NextApiHandler } from "next";
    
    const data = [
      { id: 1, name: "Wan" },
      { id: 2, name: "Zhen" },
      { id: 3, name: "Tian" }
    ];
    
    const user: NextApiHandler = (req, res) => {
      const { id } = req.query;
      const userData = data.find(x => String(x.id) === String(id));
    
      if (userData) {
        res.status(200).json(userData);
      } else {
        res.status(404).end();
      }
    };
    
    export default user;

    pages/user/[id].tsx:

    import { useRouter } from "next/router";
    import { SimpleGrid, Text, Alert, Flex, Heading } from "@chakra-ui/core";
    import useSWR from "swr";
    
    const fetcher = async (url: string) => {
      const res = await fetch(url);
      if (!res.ok) {
        throw Error("Yo that's NOT OK!!!");
      }
      const data: {
        id: string;
        name: string;
        email: string;
      } = await res.json();
    
      return data;
    };
    
    const UserData = () => {
      const router = useRouter();
      const { id } = router.query;
      const { data, error } = useSWR(`/api/user/${id}`, fetcher);
    
      if (error) {
        return <Alert status="error">Loading fail</Alert>;
      }
    
      if (!data) {
        return <Alert status="info">Loading...</Alert>;
      }
    
      return (
        <SimpleGrid columns={2} width="2xs" spacingY={4}>
          <Text fontWeight="bold" marginRight={4}>
            UserID
          </Text>
          <Text>{data.id}</Text>
    
          <Text fontWeight="bold" marginRight={4}>
            Name
          </Text>
          <Text>{data.name}</Text>
    
          <Text fontWeight="bold" marginRight={4}>
            Email
          </Text>
          <Text>{data.email}</Text>
        </SimpleGrid>
      );
    };
    
    const user = () => {
      return (
        <Flex flexDirection="column" alignItems="center">
          <Heading as="h1">User</Heading>
          <UserData />
        </Flex>
      );
    };
    
    export default user;
  • 相关阅读:
    MongoDB基础之五:游标
    SQLSERVER 中实现类似Mysql的 INSERT ON DUPLICATE KEY UPDATE
    统计C语言程序行数
    作业(一)
    无法获取有关Windows NT 组用户‘组用户’的信息,错误代码0x5(Microsoft SQL Server,错误:15404)
    DELETE与TRUNCATE的区别
    SQL Server显式事务与隐式事务
    SQL Server去掉字段内的双引号
    AlwaysOn与数据库镜像端点问题
    AlwaysOn数据同步暂停及回退技术
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12624213.html
Copyright © 2011-2022 走看看