小编Duy*_*nor的帖子

Next.js 从 SSG 中的服务器获取 HOC 中的数据

我用Next.js 9.3.1.
在带有 SSR 的旧应用程序中。我可以getInitialProps在 HOC 组件中运行(而不是在页面中),所以我可以从 HOC 组件和页面中的服务器获取数据。像这样https://gist.github.com/whoisryosuke/d034d3eaa0556e86349fb2634788a7a1

例子 :

export default function withLayout(ComposedComponent) {
    return class WithLayout extends Component {

        static async getInitialProps(ctx) {
            console.log('ctxlayout fire');
            const { reduxStore, req } = ctx || {}
            const isServer = !!req
            reduxStore.dispatch(actions.serverRenderClock(isServer))

            if (isServer) await reduxStore.dispatch(navigationActions.getListMenuAction('menu'));
            // Check if Page has a `getInitialProps`; if so, call it.
            const pageProps = ComposedComponent.getInitialProps && await ComposedComponent.getInitialProps(ctx);
            // Return props.
            return { ...pageProps }
        }

        render() {
            return …
Run Code Online (Sandbox Code Playgroud)

static get server-side-rendering higher-order-components next.js

15
推荐指数
1
解决办法
2148
查看次数