未捕获的引用错误:无法在初始化之前访问'__WEBPACK_DEFAULT_EXPORT__'[React]

2skhul33  于 2023-10-19  发布在  Webpack
关注(0)|答案(1)|浏览(287)

我得到这个错误

  1. Uncaught ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization

我已经检查了循环依赖,但找不到。我给你下面几乎所有的代码。告诉我是怎么回事,因为在本地主机上没有任何东西被渲染,它只是一个白色屏幕。

App.js

  1. import React from "react";
  2. import { useEffect } from "react";
  3. import "./App.css";
  4. import HomeScreen from "./pages/HomeScreen";
  5. import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
  6. import Login from "./pages/Login";
  7. import { auth } from "./firebase";
  8. import { useDispatch, useSelector } from "react-redux";
  9. import { login, logout, selectUser } from "./features/userSlice";
  10. import Profile from "./pages/Profile";
  11. export default function App() {
  12. const user = useSelector(selectUser);
  13. const dispatch = useDispatch();
  14. useEffect(() => {
  15. const unsubscribe = auth.onAuthStateChanged((userAuth) => {
  16. if (userAuth) {
  17. //logged in
  18. console.log(userAuth);
  19. dispatch(
  20. login({
  21. uid: userAuth.uid,
  22. email: userAuth.email,
  23. })
  24. );
  25. } else {
  26. //logged out
  27. dispatch(logout());
  28. }
  29. });
  30. return unsubscribe;
  31. }, []);
  32. return (
  33. <div className='app'>
  34. <Router>
  35. {!user ? (
  36. <Login />
  37. ) : (
  38. <Routes>
  39. <Route path='/profile'>
  40. <Profile />
  41. </Route>
  42. <Route exact path='/'>
  43. <HomeScreen />
  44. </Route>
  45. </Routes>
  46. )}
  47. </Router>
  48. </div>
  49. );
  50. }

axios.js

  1. import axios from "axios";
  2. const instance = axios.create({
  3. baseURL: "https://api.themoviedb.org/3",
  4. });
  5. export default instance;

firebase.js

  1. import firebase from "./firebase";
  2. const firebaseConfig = {
  3. apiKey: "0",
  4. authDomain: "",
  5. projectId: "",
  6. storageBucket: "",
  7. messagingSenderId: "",
  8. appId: "",
  9. };
  10. const firebaseApp = firebase.initializeApp(firebaseConfig);
  11. const db = firebaseApp.firestore();
  12. const auth = firebase.auth();
  13. export { auth };
  14. export default db;

HomeScreen.js

  1. import React from "react";
  2. import Banner from "../Banner";
  3. import Navbar from "../Navbar";
  4. import requests from "../Requests";
  5. import Row from "../Row";
  6. import "./HomeScreen.css";
  7. export default function HomeScreen() {
  8. return (
  9. <div className='homeScreen'>
  10. <Navbar />
  11. <Banner />
  12. <Row
  13. title='Netflix Originals'
  14. fetchUrl={requests.fetchNetflixOriginals}
  15. isLargeRow
  16. />
  17. <Row title='Top Rated' fetchUrl={requests.fetchTopRated} />
  18. <Row title='Trending Now' fetchUrl={requests.fetchTrending} />
  19. <Row title='Action Movies' fetchUrl={requests.fetchActionMovies} />
  20. <Row title='Comedy Movies' fetchUrl={requests.fetchComedyMovies} />
  21. <Row title='Documentries' fetchUrl={requests.fetchDocumantaries} />
  22. <Row title='Horror Movies' fetchUrl={requests.fetchHorrorMovies} />
  23. <Row title='Romance Movies' fetchUrl={requests.fetchRomanceMovies} />
  24. </div>
  25. );
  26. }

Login.js

  1. import React from "react";
  2. import { useState } from "react";
  3. import Signup from "./Signup";
  4. import "./Login.css";
  5. export default function Login() {
  6. const [signIn, setSignIn] = useState(false);
  7. return (
  8. <div className='login'>
  9. <div className='login_bg'>
  10. <img
  11. className='login_logo'
  12. src='https://assets.stickpng.com/images/580b57fcd9996e24bc43c529.png'
  13. alt='netflix logo'
  14. />
  15. <button onClick={() => setSignIn(true)} className='login_button'>
  16. Sign In
  17. </button>
  18. <div className='login_gradient' />
  19. </div>
  20. <div className='login_body'>
  21. {signIn ? (
  22. <Signup />
  23. ) : (
  24. <>
  25. <h1>Unlimited movies, TV shows and more.</h1>
  26. <h2>Watch anywhere. Cancel anytime.</h2>
  27. <h3>
  28. Ready to watch? Enter your email to create or restart your
  29. membership.
  30. </h3>
  31. <div className='login_input'>
  32. <form>
  33. <input type='email' placeholder='EmailAddress' />
  34. <button
  35. onClick={() => setSignIn(true)}
  36. className='login_getStarted'
  37. >
  38. Get Started
  39. </button>
  40. </form>
  41. </div>
  42. </>
  43. )}
  44. </div>
  45. </div>
  46. );
  47. }

Signup.js

  1. import React from "react";
  2. import { useRef } from "react";
  3. import { auth } from "../firebase";
  4. import "./Signup.css";
  5. export default function Signup() {
  6. const emailRef = useRef(null);
  7. const passwordRef = useRef(null);
  8. const register = (e) => {
  9. e.preventDefault();
  10. auth
  11. .createUserWithEmailAndPassword(
  12. emailRef.current.value,
  13. passwordRef.current.value
  14. )
  15. .then((authUser) => {
  16. console.log(authUser);
  17. })
  18. .catch((error) => {
  19. alert(error.message);
  20. });
  21. };
  22. const signIn = (e) => {
  23. e.preventDefault();
  24. auth
  25. .signInWithEmailAndPassword(
  26. emailRef.current.value,
  27. passwordRef.current.value
  28. )
  29. .then((authUser) => {
  30. console.log(authUser);
  31. })
  32. .catch((error) => {
  33. alert(error.message);
  34. });
  35. };
  36. return (
  37. <div className='signup'>
  38. <form>
  39. <h1>Sign In</h1>
  40. <input ref={emailRef} placeholder='Email' type='email' />
  41. <input ref={passwordRef} placeholder='Password' type='password' />
  42. <button type='submit'>Sign In</button>
  43. <h4>
  44. <span className='signup_gray'>New to Netflix? </span>{" "}
  45. <span className='signup_link'>Sign Up now.</span>
  46. </h4>
  47. </form>
  48. </div>
  49. );
  50. }

Banner.js

  1. import React from "react";
  2. import { useEffect, useState } from "react";
  3. import axios from "axios";
  4. import requests from "./Requests";
  5. import "./Banner.css";
  6. export default function Banner() {
  7. const [movie, setMovie] = useState([]);
  8. useEffect(() => {
  9. async function fetchData() {
  10. const request = await axios.get(requests.fetchNetflixOriginals);
  11. setMovie(
  12. request.data.results[
  13. Math.floor(Math.random() * request.data.results.length - 1)
  14. ]
  15. );
  16. return request;
  17. }
  18. fetchData();
  19. }, []);
  20. console.log(movie);
  21. function truncate(string, n) {
  22. return string?.length > n ? string.substr(0, n - 1) + "..." : string;
  23. }
  24. return (
  25. <header
  26. className='banner'
  27. style={{
  28. // height: "100vh",
  29. backgroundSize: "cover",
  30. backgroundImage: `url("https://image.tmdb.org/t/p/original/${movie?.backdrop_path}")`,
  31. backgroundPosition: "top center",
  32. backgroundRepeat: "no-repeat",
  33. }}
  34. >
  35. <div className='banner_contents'>
  36. <h1 className='banner_title'>
  37. {movie?.title || movie?.name || movie?.original_name}
  38. </h1>
  39. <div className='banner_buttons'>
  40. <button className='banner_button_play'>Play</button>
  41. <button className='banner_button'>My List</button>
  42. </div>
  43. <h1 className='banner_description'>{truncate(movie?.overview, 150)}</h1>
  44. </div>
  45. <div className='banner--fadeBottom' />
  46. </header>
  47. );
  48. }

Row.js

  1. import React from "react";
  2. import { useEffect, useState } from "react";
  3. import axios from "axios";
  4. import "./Row.css";
  5. export default function Row({ title, fetchUrl, isLargeRow = false }) {
  6. const [movies, setMovies] = useState([]);
  7. const base_url = "https://image.tmdb.org/t/p/original/";
  8. useEffect(() => {
  9. async function fetchData() {
  10. const request = await axios.get(fetchUrl);
  11. setMovies(request.data.results);
  12. return request;
  13. }
  14. fetchData();
  15. }, [fetchUrl]);
  16. return (
  17. <div className='row'>
  18. <h2>{title}</h2>
  19. <div className='row_posters'>
  20. {movies.map(
  21. (movie) =>
  22. ((isLargeRow && movie.poster_path) ||
  23. (!isLargeRow && movie.backdrop_path)) && (
  24. <img
  25. className={`row_poster ${isLargeRow && "row_posterLarge"}`}
  26. key={movie.id}
  27. src={`${base_url}${
  28. isLargeRow ? movie.poster_path : movie.backdrop_path
  29. }`}
  30. alt={movie.name}
  31. />
  32. )
  33. )}
  34. </div>
  35. </div>
  36. );
  37. }

store.js

  1. import { configureStore } from "l@reduxjs/toolkit";
  2. import userReducer from "../features/userSlice.js";
  3. export default configureStore({
  4. reducer: {
  5. user: userReducer,
  6. },
  7. });

userSlice.js

  1. import { createSlice } from "@reduxjs/toolkit";
  2. export const userSlice = createSlice({
  3. name: "user",
  4. initialState: {
  5. user: 0,
  6. },
  7. reducers: {
  8. login: (state, action) => {
  9. state.user = action.payload;
  10. },
  11. logout: (state) => {
  12. state.user = null;
  13. },
  14. },
  15. });
  16. export const { login, logout } = userSlice.actions;
  17. export const selectUser = (state) => state.user.user;
  18. export default userSlice.reducer;

我认为这是因为axios,所以我给了所有的文件,其中包含axios在它。

fivyi3re

fivyi3re1#

此错误与循环依赖项或不正确的导入/导出用法有关,但在代码中,我没有看到任何这些错误
我唯一能想到的是firebase.js文件中的firebase导入不正确。
您正在从自身导入firebase,这可能会导致问题。
更改此选项:

  1. import firebase from "./firebase";

对此:

  1. import firebase from 'firebase/compat/app';
  2. import "firebase/compat/auth";
  3. import 'firebase/compat/firestore';

顺便说一句,请确保您的axios导入和使用是正确的-在HomeScreen.js和其他组件中导入的请求对象定义正确,并包含适当的URL。

相关问题