appbaseio / reactivesearch

Search UI components for React and Vue
https://opensource.appbase.io/reactivesearch
Apache License 2.0
4.89k stars 471 forks source link

SSR: ctx not passed to child components #456

Closed coommark closed 5 years ago

coommark commented 5 years ago

In the SSR samples, one can access the Next.JS context like this: static async getInitialProps(ctx). So when you console.log(ctx) you can access both the reqand rescontext objects. However, when you load a child component and try to access the context in static async getInitialProps(ctx), the ctx is empty.

Could this be a problem with the return statement of the parent's return statement of the getInitialProps? Is it swallowing up the ctx and not passing it down?

return {
            store: await initReactivesearch(
                [
                    {
                        ...homeCategorySearchProps,
                        type: 'CategorySearch',
                        source: CategorySearch,
                    },
                    {
                        ...reactiveListProps,
                        type: 'ReactiveList',
                        source: ReactiveList,
                    },
                ],
                null,
                settings,
            ),
        };

Please how do I pass the ctx down to a child component such as a Navigation component so I can extract session information from the ctx?

Thank you.

coommark commented 5 years ago

Ah! I see that this is a Next.js issue, not reactivesearch. In one of their response they said "We don't have plans to add support for calling getInitialProps in nested components."

That settles it.

divyanshu013 commented 5 years ago

Yup, as far as I remember getInitialProps can only be used at the page level. For passing it further down you could perhaps use react's context api if its at a nested level.