J
J
JekaHC2022-04-19 23:30:44
JavaScript
JekaHC, 2022-04-19 23:30:44

Why is the component not updated after import?

I have react component

MyAccount.jsx

import "./myAccount.sass";

function MyAccount() {
  return (
    <div className="MyAccount">
      <h3 className="MyAccount_title">Hi, *USERNAME*</h3>
      <div className="MyAccount_logout-container">
        <button className="MyAccount_logout">Logout...</button>
      </div>
      <hr className="MyAccount_hr" />
      <h4 className="MyAccount_subtitle">Your reviews:</h4>
    </div>
  );
}

export default MyAccount;


I call it in main component MainPage.jsx

import { useState, useEffect } from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";

import {
  collection,
  getDocs,
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-auth.js";
import { db, auth } from "../firebaseConfig";

import Hotels from "./Hotels/Hotels";
import CreateReview from "./CreateReview/CreateReview";
import MyAccount from "./MyAccount/MyAccount";
import WelcomePage from "./WelcomePage/WelcomePage";
import NavBar from "./NavBar/NavBar";

function MainPage() {
  const [hotels, setHotels] = useState([]);
  const [authentication, setAuthentication] = useState(false);

  async function fetchHotels() {
    const _hotels = [];
    const querySnapshot = await getDocs(collection(db, "reviews"));
    querySnapshot.forEach((doc) => {
      _hotels.push(doc.data());
    });
    console.log("Data is fetched!");
    setHotels(_hotels);
  }

  useEffect(() => {
    fetchHotels();
  }, []);

  function isAuthenticated() {
    onAuthStateChanged(auth, (user) => {
      if (user) {
        setAuthentication(true);
        console.log("User is signed in");
      } else {
        setAuthentication(false);
        console.log("User is signed out");
      }
    });
  }

  useEffect(() => {
    isAuthenticated();
  }, []);

  return (
    <div>
      {authentication ? (
        <BrowserRouter>
          <NavBar />
          <div className="container">
            <Routes>
              <Route
                exact
                path="/CreateReview"
                element={<CreateReview />}
              ></Route>
              <Route
                index
                exact
                path="/Hotels"
                element={<Hotels hotels={hotels} />}
              ></Route>
              <Route
                exact
                path="/MyAccount"
                element={<MyAccount sraka={setAuthentication} />}
              ></Route>
              <Route path="*" element={<Hotels hotels={hotels} />} />
            </Routes>
          </div>
        </BrowserRouter>
      ) : (
        <div className="container">
          <WelcomePage />
        </div>
      )}
    </div>
  );
}

export default MainPage;


In this scenario, the application reacts to changes and is updated.

As soon as I add these imports to the MyAccount.jsx component, the application stops responding to changes, and changes in MyAccount.jsx (and not only) are only visible after manually reloading the application:

import {
  collection,
  getDocs,
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-auth.js";
import { db, auth } from "../../firebaseConfig";

import "./myAccount.sass";

function MyAccount() {
  
  return (
    <div className="MyAccount">
      <h3 className="MyAccount_title">Hi, *USERNAME*</h3>
      <div className="MyAccount_logout-container">
        <button className="MyAccount_logout">Logout...</button>
      </div>
      <hr className="MyAccount_hr" />
      <h4 className="MyAccount_subtitle">Your reviews:</h4>
    </div>
  );
}

export default MyAccount;


There are no errors in the console.

As soon as I remove this from the MyAccount.jsx component:

import {
  collection,
  getDocs,
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-auth.js";
import { db, auth } from "../../firebaseConfig";


the app is reacting to changes again...

What's the problem?

GitHub Repo : link

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question