-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
"window is not defined" when importing in next.js #1639
Comments
@ilyador — Unfortunately I haven't used Isotope with Next.js, and it's been quite awhile since I used it with React at all. I checked my current webpack configuration with Isotope, and it looks like I removed the old "window" hack, but looking at an older site, I was using something like this...
Now, again, this is an older site and that config doesn't work well with the current version of webpack. You might have to re-formulate it to something like:
I have no tested this, and don't often use the Let me know if you can get this working at all. This is the best answer I have off the top of my head, and I'm not sure that this has actually been configured to work with Next.js (doesn't mean it won't work though). Happy to help you debug a bit if you can't get it working. |
Thank you @thesublimeobject but is there any way to do it without touching webpack? I believe I'll have to eject the next.js project to gain access to the webpack config. |
@ilyador — I don't believe so, unfortunately. You have to remember that this library was originally built years ago, and was designed for pretty basic HTML applications. Honestly, I'm not even sure this will work for you in react...for some reason I thought I remembered using it once, but going back and checking a few references, I'm doubting that I ever did. This is just the tradeoff of using something like Next.js or create-react-app...most things will work for you very easily if you are using a fairly standard setup, but for something like this that wasn't built for that framework, in order to use it you'll need to dig into some of the minutia. That said, I would probably recommend just not using this library for that project, as strange as it might sound. I think you'll end up running into a lot of trouble that probably won't be worth it. React, in itself, has very natural filtering and sorting applications. Given the way it works, you can actually implement something like Isotope without needing an external library by just tracking the state of your filters in the state and then filtering the items on re-render. I actually have an example of a very recent prototype I built in Next.js for a client wanting to convert their site. I will post the code and some comments below. If you have any further questions, just follow up! Below are four separate code blocks. The first is the main component for the grid. The second two are helper functions, which you can see used in the main component (used to get products/categories and form filter groups, etc.). The last block is the component for the filters menu, for which I just used a material-ui form. To hopefully make this a little easier to read, I removed a lot of the extraneous information/imports, so there might be some references missing (like styled components, etc.), but I didn't think these would be helpful to actually understanding what's going on here. So, on load, we build the filter groups from the categories, and then we use those groups via useState to track which filters are active. Then, we use
|
you need to check before calling isotope |
Do not import statically in module header, instead dynamically in useEffect() which always run in client.
|
I am using isotope-layout on a next.js project (not sure if related)
Whenever I refresh ht page that calls
new Isotope
I get this:The strange thing is that when I comment out
new Isotope
and uncomment, it works because the hot reload of next.js only reloads part of the page.Any ideas why this is happening?
The text was updated successfully, but these errors were encountered: