I need to access the window
object in my react component to get something from the query string. This is what my component looks like:
export function MyComponent() {
return (
<div>
Display the qs: {(window && window.location.search) || 'nothing'}
</div>
)
}
This is all fine and well if I run the app and then visit the page where I need to access the window, but if I start the app on the page I need the window, I get the following error:
ReferenceError: window is not defined
Most the solutions I see so far make use of the componentWillMount
to solve the issue. So my question is, how do I solve this issue in the functional component? Or what is the best way to do so without using the lifecycle methods?
Not sure if this is relevant but I am using NextJS as well.