Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gatsby Build - SSR failed #732

Open
lorraineS opened this issue Aug 25, 2021 · 4 comments
Open

Gatsby Build - SSR failed #732

lorraineS opened this issue Aug 25, 2021 · 4 comments

Comments

@lorraineS
Copy link

lorraineS commented Aug 25, 2021

Hello,

I've been trying to use react-carousel in my project, while it works in development, it doesn't when I try to build.

I have read all the posts that deal with this problem and I manage to partially solve it EXCEPT when I have to use slidesToShowPlugin and autoplayPlugin.

Here is my import which works locally :
import Carousel, { slidesToShowPlugin, autoplayPlugin } from '@brainhubeu/react-carousel';

I changed it to this one:
const Carousel = loadable(() => import('@brainhubeu/react-carousel'))

show me errors :

45:26  error  'autoplayPlugin' is not defined      no-undef
56:29  error  'slidesToShowPlugin' is not defined  no-undef

My Carousel component refers to several plugins:

<Carousel
            offset={30}
            minDraggableOffset={0}
            keepDirectionWhenDragging={true}
            plugins={[
              {
                resolve: autoplayPlugin,
                options: {
                  interval: 4000,
                }
              },
            ]}
            animationSpeed={1000}
            breakpoints={{
              900: {
                plugins: [
                 {
                   resolve: slidesToShowPlugin,
                   options: {
                    numberOfSlides: 1
                   }
                 },
               ]
              }
            }}
          >
            {props.list.map(item) => {
              return (
                <CardV3
                  title={item.title}
                />
              )
            })}
</Carousel>

I specify that I use Gatsby and not Next.js. So I used the "loadable" library which is mentioned by another user (last message here : #287) and which works well, except when it is also necessary to import plugins from Carousel.

Thanks

@amitdhiman000
Copy link

I can't use it in nextjs

ReferenceError: window is not defined
    at Yo node_modules/@brainhubeu/react-carousel/lib/react-carousel.js:2:100449

It would be great to have SSR support.

@KennStack01
Copy link

Facing the same issue: undefined (window) in SSR

@mclngl
Copy link

mclngl commented Oct 7, 2021

In these cases I usually wrap it into something like this:

`function Page({ render = "effect", children } = {}) {
const [hasRenderedOnClient, setHasRendered] = React.useState(false);

React.useEffect(() => {
setHasRendered(true);
}, []);

if (render === "effect" && !hasRenderedOnClient) {
return null;
}

if (render === "effect-seo" && !hasRenderedOnClient) {
return <div style={{ visibility: "hidden" }}>{children};
}

return <>{children}</>;`

so it renders when useEffect kicks in on the client.
Only downside is that js has to be downloaded in order to see the carousel

@ghost
Copy link

ghost commented Nov 25, 2021

why do you call this react-carousel? carousel doesn't work with react, gatsby, or next..there are 3 more closed(!) window issues related to this...dynamic import doesn't bring the module and why should we dynamic import anyway...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants