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

Component breaks SSR #4

Closed
avnersorek opened this issue Jan 23, 2017 · 5 comments
Closed

Component breaks SSR #4

avnersorek opened this issue Jan 23, 2017 · 5 comments

Comments

@avnersorek
Copy link

When trying to render this on the server I get -

ReferenceError: window is not defined
  at Object.<anonymous> (/home/nodejs/app/node_modules/flickity/js/index.js:39:5)
@theolampert
Copy link
Collaborator

Closing this in favour of #5

@danielmahon
Copy link
Contributor

For anyone that needs, I was able to get SSR working with something like the following (Gatsby example):

class Testimonials extends Component {
  state = { Flickity: null };
  constructor(props) {
    super(props);
    if (typeof window !== 'undefined') {
      const Flickity = require('react-flickity-component');
      this.state.Flickity = Flickity;
    }
  }
  render() {
    const { Flickity } = this.state;
    const { data: { testimonials } } = this.props;
    return (
      <section className="text-center imagebg" data-overlay="8">
        <div className="background-image-holder">
          <img alt="background" src={bgImage} />
        </div>
        <div className="container">
          <div className="row">
            <div className="col-lg-8 col-md-10">
              <div className="slider">
                {Flickity && (
                  <Flickity
                    className={'slides'}
                    elementType={'ul'}
                    options={{
                      wrapAround: true,
                      autoPlay: true,
                      draggable: true,
                      pageDots: false,
                      prevNextButtons: false,
                    }}>
                    {testimonials.edges.map(({ node }) => (
                      <li key={node.id}>
                        <div className="testimonial">
                          <blockquote>{node.frontmatter.quote}</blockquote>
                          <h5>{node.frontmatter.title}</h5>
                          <span className="type--fade">
                            {node.frontmatter.location}
                          </span>
                        </div>
                      </li>
                    ))}
                  </Flickity>
                )}
              </div>
            </div>
          </div>
        </div>
      </section>
    );
  }
}

@humbertqz
Copy link

humbertqz commented Mar 21, 2019

I'm using this in the import and works:

const isBrowser = typeof window !== 'undefined'; const Flickity = isBrowser ? require('react-flickity-component') : 'div';

@kierangillen
Copy link

const Flickity =
  typeof window !== "undefined"
    ? require("react-flickity-component")
    : () => null

Use this above your component.

@frazerf
Copy link

frazerf commented Feb 27, 2020

Hey @kierangillen , I've just stumbled across your answer. I've got the same problem re. window error. Are you adding this directly below the imports? And are you excluding import Flickity from 'react-flickity-component'

Thanks

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

6 participants