Posted By: Anonymous
I’m working on a prototype for a web app and I chose NextJS because I wanted to learn it better, but I realize I’m not using it in the “standard” way. I started from the Next + Material-UI example here: https://github.com/mui-org/material-ui/tree/master/examples/nextjs and all was good.
But now, I’m trying to persist data using
sessionStorage, and I’m finding it difficult to make this work without being able to use the
componentDidMount lifecycle event at the page-level. Next’s documentation says that projects with a custom _document.js file won’t have the
componentDidMount method client-side. My project uses a custom document file due to Material-UI. It seems like it’s there to support server-side rendering of css-in-js frameworks.
If I don’t need server-side rendering, is there a way I can keep Material-UI working, but do away with the custom _document.js file so I can use
You should be fine using both
sessionStorage and a custom document.
componentDidMount (and any other lifecycle method) is not working only for
_document.js as this is not rendered client side.
sessionStorage is part of the
window so you will be fine using it on any other page/component using the