gpt4 book ai didi

javascript - 如何在 _app.js 中使用 getStaticProps 在 Next.js 的每个页面中显示从 api 获取的数据

转载 作者:行者123 更新时间:2023-12-04 11:22:58 27 4
gpt4 key购买 nike

具体来说,我在导航栏上显示来自 API 端点的数据。我想使用 getStaticProps 在所有页面上获取这些数据。但问题是我需要在所有页面上执行 getStaticProps 。
我们可以做这样的事情并获取所有页面上的数据作为 Prop 吗?

//pages/_app.js
function MyApp({ Component, pageProps, navs }) {
return (
<>
<Component {...pageProps} navs={navs} />
</>
);
}
export async function getStaticProps() {
const res = await api.get("/api/v1/navs");
const navs = res.data;
return {
props: {
navs,
},
// revalidate: 60, // In seconds
};
}

export default MyApp;

这样做的替代方法是什么?有没有办法管理全局状态,以便所有页面都使用它?我不认为我们也可以使用 Context API 为所有页面提供数据。

最佳答案

问题
NextJS 当前不支持 _app.js 中的生命周期方法.
解决方案
由于上述内容尚不支持,您需要创建一个可重复使用的 getStaticProps函数并从 导出所有页面 .不幸的是,这确实意味着一些 WET 代码;但是,您可以通过创建 HOC 来减少一些样板。它包装了页面并导出了一个 getStaticProps 函数。
备择方案
您可以使用 getInitialProps_app.js 内文件。不幸的是,这会禁用自动静态优化 跨整个应用程序 .
演示
Edit Static Optimized List
代码
组件/导航

import * as React from "react";
import Link from "next/link";
import { nav, navItem } from "./Navigation.module.css";

const Navigation = () => (
<div className={nav}>
{[
{ title: "Home", url: "/" },
{ title: "About", url: "/about" },
{ title: "Help", url: "/help" }
].map(({ title, url }) => (
<div className={navItem} key={title}>
<Link href={url}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<a>{title}</a>
</Link>
</div>
))}
</div>
);

export default Navigation;
组件/用户列表
import * as React from "react";
import isEmpty from "lodash.isempty";
import { noUsers, title, userList, user } from "./UsersList.module.css";

const UsersList = ({ error, users, retrieved }) =>
!retrieved ? (
<p>Loading...</p>
) : !isEmpty(users) ? (
<div className={userList}>
<h1 className={title}>Statically Optimized User List</h1>
{users.map(({ name }) => (
<div className={user} key={name}>
{name}
</div>
))}
</div>
) : (
<div className={noUsers}>{error || "Failed to load users list"}</div>
);

export default UsersList;
容器/withUsersList
import * as React from "react";
import axios from "axios";
import UsersList from "../../components/UsersList";

/**
* A HOC that wraps a page and adds the UserList component to it.
*
* @function withUsersList
* @param Component - a React component (page)
* @returns {ReactElement}
* @example withUsersList(Component)
*/
const withUsersList = (Component) => {
const wrappedComponent = (props) => (
<>
<UsersList {...props} />
<Component {...props} />
</>
);

return wrappedComponent;
};

export const getStaticProps = async () => {
try {
const res = await axios.get("https://jsonplaceholder.typicode.com/users");

return {
props: {
retrieved: true,
users: res.data,
error: ""
}
};
} catch (error) {
return {
props: {
retrieved: true,
users: [],
error: error.toString()
}
};
}
};

export default withUsersList;
pages/_app.js
import * as React from "react";
import Navigation from "../components/Navigation";

const App = ({ Component, pageProps }) => (
<>
<Navigation />
<Component {...pageProps} />
</>
);

export default App;
页数/关于
import withUsersList, { getStaticProps } from "../containers/withUsersList";

const AboutPage = () => <div>About Us.</div>;

export { getStaticProps };

export default withUsersList(AboutPage);
页面/帮助
import withUsersList, { getStaticProps } from "../containers/withUsersList";

const HelpPage = () => <div>Find Help Here.</div>;

export { getStaticProps };

export default withUsersList(HelpPage);
页数/索引
import withUsersList, { getStaticProps } from "../containers/withUsersList";

const IndexPage = () => <div>Hello World.</div>;

export { getStaticProps };

export default withUsersList(IndexPage);

关于javascript - 如何在 _app.js 中使用 getStaticProps 在 Next.js 的每个页面中显示从 api 获取的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67640024/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com