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

Kus*_*oon 6 reactjs fetch-api next.js swr next.js13

我使用 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;
Run Code Online (Sandbox Code Playgroud)

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

小智 5

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