1

Hi I am using Nextjs and I am getting this error:

enter image description here

Here is my code:

import React, { createContext, useState, useContext, useEffect } from "react";
import useAuth from '../hooks/useAuth';
import axios from "axios";


// if (typeof window !== "undefined") {
//     const code = new URLSearchParams (window.location.search).get("code")
//   }

const code = new URLSearchParams (window.location.search).get("code")

export const SpotifyContext = createContext()

function SpotifyProvider({ children }){
    const { accessToken, connecting, SignOut, error, setError, tokenExpired } = useAuth(code)

    const headers = {
        Authorization: `Bearer ${accessToken}`,
        'Content-Type': 'application/json',
    }
Jose Lora
  • 1,392
  • 4
  • 12
  • 18
cccccc03
  • 11
  • 2

1 Answers1

1

I think https://blog.sethcorker.com/question/how-to-solve-referenceerror-next-js-window-is-not-defined/ might help you. The important point is to rely on the useEffect, conveniently hooks aren’t run when doing server-side rendering. Wrapping the usage of window inside a useEffect that is triggered on mount means the server will never execute it and the client will execute it after hydration.

top16Dev
  • 50
  • 8