尝试导入错误:“swr”不包含默认导出(导入为“useSWR”)。 - nextjs 13

问题描述 投票:0回答:3

我使用 nextjs 13.4.7 制作项目,并已在 3 台 PC 中安装 swr,但出现了相同的错误: 尝试导入错误:“swr”不包含默认导出(导入为“useSWR”)。 错误 TypeError: (0 , swr__WEBPACK_IMPORTED_MODULE_2__.default) 不是一个函数

我的 swr 版本是“swr”:“^2.2.0”

我这样使用swr:

import useSWR from 'swr';

function Todos() {

  // Define the fetcher function to fetch data from the API
  const fetcher = (url) => fetch(url).then((res) => res.json());

  // Use SWR to fetch data from the API
  const { data, error } = useSWR('https://jsonplaceholder.typicode.com/todos/', fetcher);

  if (error) return <div>Failed to load todos</div>;
  if (!data) return <div>Loading todos...</div>;

  return (
    <div>
      <h1>Todos</h1>
      {data.map((todo) => (
        <div key={todo.id}>
          <h3>{todo.title}</h3>
          <p>{todo.completed ? 'Completed' : 'Not completed'}</p>
        </div>
      ))}
    </div>
  );
}

export default Todos;

我希望有人可以帮助我修复它或将其报告为 swr 的错误

reactjs next.js fetch-api next.js13 swr
3个回答
3
投票

我认为你忘记在代码顶部输入“使用客户端”


0
投票

尝试使用页面顶部的“使用客户端”。我在尝试使用钩子时也遇到类似的错误。

(0, swr__WEBPACK_IMPORTED_MODULE_4__.default) is not a function

我发现我忘记使用“使用客户端”。我将“使用客户端”放在顶部,然后错误就解决了。


0
投票

使用“使用客户端”。会起作用的

© www.soinside.com 2019 - 2024. All rights reserved.