我给了remix一个很好的第一次尝试,我想我喜欢它的数据处理方法。然而,我正面临着数据返回"未定义"的问题。
import { LoaderFunction } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import axios from 'axios';
const url = 'https://jsonplaceholder.typicode.com/users'
export async function Members(){
const {data} = await axios.get(url);
return data
} //I am sure it returns the expected data when I call on this function.
export const loader: LoaderFunction = async () => {
const response = await Members()
return response
};
export const Architects = () => {
const members = useLoaderData()
console.log(members)
return (
....
)
}
我到底错过了什么?给我点提示吧。使用其他基于react的方法不是remix的
是不明智的。这甚至不是一个问题,而是在编写方法时的盗用。
对于那些可能犯这种愚蠢错误的人,请确保你在你的路由中调用了这些remix助手。
它不会像我试图做的那样在任何组件文件中工作。加载器和useLoaderData以及大多数remix的方法主要是服务器端。以上代码没有任何问题。问题就出在哪里
感谢所有看过这篇文章的人。
我还想指出,LoaderFunction应该这样导入:
import type { LoaderFunction } from "@remix-run/node";