NextJS - 如何组合多个 getServerSideProps 包装器?

时间:2021-02-17 22:13:45

标签: javascript reactjs next.js getserversideprops

我在 NextJs 应用中使用了两个库:next-firebase-authnext-redux-wrapper。它们都要求我用它们各自的函数包装 getServerSideProps

对于next-firebase-auth

export const getServerSideProps = withAuthUserSSR()(async ({ AuthUser }) => {
    // Some code
})

对于next-redux-wrapper

export const getServerSideProps = wrapper.getServerSideProps(
    ({store}) => {
        // Some code
    }
);

两者都单独工作,但我无法让两者同时工作。 NextJs 只允许 getServerSideProps 声明一次。是否有可能以某种方式组合多个包装器?

1 个答案:

答案 0 :(得分:0)

您可以一个接一个地链接包装器。内部函数将包含两者传递的额外道具。

export const getServerSideProps = withAuthUserSSR()(wrapper.getServerSideProps(
    ({ AuthUser, store, res, req }) => {
        // Some code
    }
))