Next.js: set up jQuery as global object

Created on 7 Jul 2017  路  3Comments  路  Source: vercel/next.js

Hi,
How can i set up jQuery object as global in my app?
When I'm using: window.jQuery = window.$ = require('jquery') if i go to different route like /about and refresh the page, then I recive the some error msg like "window object is undefined".

Or maybe some of you have some example repo with how to integrate next and materialize.css ?

Thank you.

Most helpful comment

@wjelen26 you can't do that because when the page is being server rendered the object window doesn't exists, you can import jQuery and then set it as a global object in the componentDidMount of a layout component, that way you can be sure the window object exists.

All 3 comments

Use https://github.com/zeit/next.js/tree/v3-beta/examples/layout-component and define it in the layout component. That should work :)

doesnt work, same issue

@wjelen26 you can't do that because when the page is being server rendered the object window doesn't exists, you can import jQuery and then set it as a global object in the componentDidMount of a layout component, that way you can be sure the window object exists.

Was this page helpful?
0 / 5 - 0 ratings