如何安全地将getInitialProps替换为getServerSideProps / getStaticProps

时间:2020-10-26 17:19:08

标签: next.js getserversideprops

我们所有人都很熟悉以下句子:You should not use getInitialProps, but getServerSideProps instead。 我已经使用getInitialProps构建了我的应用程序,现在我面临以下问题:“我应该将getInitialProps替换为getServerSideProps吗?”,如果是这样,如何以正确的方式进行操作而又不损坏我的应用程序?

今天,我在一些重要的地方使用getInitialProps

  1. 在我的自定义_app.js中为react-redux
class MyApp extends App {
    static async getInitialProps({ Component, ctx }) {
        return {
            pageProps: {
                ...(Component.getInitialProps
                    ? await Component.getInitialProps(ctx)
                    : {})
            }
        };
    }

    render() {
        const { Component, pageProps, store } = this.props;
        return (
            <>
                <Provider store={store}>
                    <Component {...pageProps} />
                </Provider>
            </>
        );
    }
}

export default withRedux(initStore)(withReduxSaga(MyApp));
  1. 在几个页面的HOC中:检查用户是否已通过身份验证(否则重定向),如果用户已通过身份验证,则初始化redux存储,以获取用户信息。

如何用getServerSideProps替换getInitialProps?

任何建议,我们将不胜感激, 谢谢

0 个答案:

没有答案
相关问题