我正在使用 React Js
和 Next Js
作为我的框架来开发服务器端渲染
React应用程序,并且我正在尝试也引用文档 ( next js doc for getServerSideProps ) 使用 getServerSideProps
方法获取初始 props。
但每次请求时我总是得到空的 props 对象作为 {}
。目前,我只是尝试在 props 中传递一个虚拟文本。
如何在初始加载时获取我的props
?
请引用下面我的代码
import React from "react";
import { Provider } from "react-redux";
import ConfigureStore from "../Store";
const Home = props => {
const store = ConfigureStore();
console.log("props", props);
return (
<Provider store={store}>
<div>My content</div>
</Provider>
);
};
// This gets called on every request
export async function getServerSideProps() {
const data = "Hello World";
return { props: data };
}
export default Home;
最佳答案
getServerSideProps
的问题就是它只能是 exported from a page 。您无法从非页面文件导出它。 getStaticProps 也是如此.
所以你不能在部分组件中使用它。
这意味着您只能直接对 /page
目录中的文件使用它。
关于javascript - 无法在 React js 中获取 `props` - 下一个 js 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60656259/