J
J
JekaHC2022-04-19 02:04:57
React
JekaHC, 2022-04-19 02:04:57

Why did the browser stop updating the result of the changed code?

Today I continued to work on the application, everything worked (I changed the code - the page was updated), but as soon as I created a new component (MyAccount.jsx) and started working with it, I noticed that updates were no longer displayed in the browser after changing the code, you need to press to reload the page. There are no errors in the console. Below I will show the main component (MainPage.jsx) and MyAccount.jsx, after the creation of which the automatic update stopped working.

MainPage.jsx

import { useState, useEffect } from "react";
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js";
import {
  getFirestore,
  collection,
  getDocs,
  addDoc,
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
import {
  getAuth,
  signInWithEmailAndPassword,
  createUserWithEmailAndPassword,
  onAuthStateChanged,
  signOut,
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-auth.js";
import { firebaseConfig, app, db, auth } from "../firebaseConfig";

import { Hotels } from "./Hotels/Hotels";
import WelcomePage from "./WelcomePage/WelcomePage";
import NavBar from "./NavBar/NavBar";
import MyAccount from "./MyAccount/MyAccount";

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.id, doc.data()]);
    });
    console.log("Data is fetched!");
    setHotels(_hotels);
  }

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

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

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

  return (
    <div className="container">
      {authentication ? (
        <>
          <NavBar />
          
          <MyAccount hotels={hotels} isauth={setAuthentication} />
        </>
      ) : (
        <WelcomePage />
      )}
    </div>
  );
}

export default MainPage;


MyAccount.jsx

import "./myAccount.sass";
import { auth } from "../../firebaseConfig";
import {
  signOut,
  onAuthStateChanged,
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-auth.js";

import { useEffect, useState } from "react";
import { Hotel } from "../Hotel/Hotel";

function MyAccount(props) {
  const { isauth } = props;
  const { hotels = [] } = props;
  const [userEmail, setUserEmail] = useState([]);

  function logout() {
    signOut(auth);
    isauth(false);
    console.log("Successful logout");
  }

  function userInfo() {
    onAuthStateChanged(auth, (user) => {
      console.log(`user info: ${user.email}"`);
      setUserEmail(user.email);
    });
  }
  useEffect(() => {
    userInfo();
  }, []);
  return (
    <div className="MyAccount">
      <h3 className="MyAccount_title">Hi, {userEmail}</h3>
      <button onClick={logout}>Logout...</button>
      <h4>Your reviews:</h4>
      <hr />
      <div className="Hotels">
        {hotels.map((hotel) => (
          <Hotel
            {...hotel[1]}
            key={hotel[0]}
            id={hotel[0]}
            userEmail={userEmail}
            filter={true}
            deleteBtn={true}
          />
        ))}
      </div>
    </div>
  );
}

export default MyAccount;


Hotel.jsx (Used in MyAccount.jsx)

import starsMap from "../../starsMap";
import { useEffect, useState } from "react";
import { db } from "../../firebaseConfig";
import {
  doc,
  deleteDoc,
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
import "./hotel.sass";

function Hotel(props) {
  const {
    name,
    img,
    localization,
    stars,
    review,
    author,
    filter = false,
    deleteBtn = false,
    userEmail = null,
    id,
  } = props;

  async function deleteReview(id) {
    console.log(`Trying to delete object with id=${id}`);
    await deleteDoc(doc(db, "reviews", id));
  }

  if (filter) {
    console.log(author, userEmail);
    if (author === userEmail) {
      return (
        <div className="Hotel_card" id="hotel_card">
          {deleteBtn ? (
            <button onClick={() => deleteReview(id)}>Delete</button>
          ) : (
            <></>
          )}
          <h2 className="Hotel_card-name">{name}</h2>
          <div className="Hotel_card_wrapper">
            <img className="Hotel_card-img" src={img} alt="hotel_img" />
            <h3 className="Hotel_card-localization">
              {/* Lat:{localization._lat}
              Long:{localization._lat} */}
              <button>Show on map</button>
            </h3>
            {starsMap.get(stars)}
          </div>
          <p className="Hotel_card-review">{review}</p>
          <h5 className="Hotel_card-author">Wroten by {author}</h5>
        </div>
      );
    }
  } else {
    return (
      <div className="Hotel_card">
        <h2 className="Hotel_card-name">{name}</h2>
        <div className="Hotel_card_wrapper">
          <img className="Hotel_card-img" src={img} alt="hotel_img" />
          <h3 className="Hotel_card-localization">
            {/* Lat:{localization._lat}
            Long:{localization._lat} */}
            <button>Show on map</button>
          </h3>
          {starsMap.get(stars)}
        </div>
        <p className="Hotel_card-review">{review}</p>
        <h5 className="Hotel_card-author">Wroten by {author}</h5>
      </div>
    );
  }
}

export { Hotel };



I noticed the problem after I made the Delete button and after clicking on it, you need to refresh the page manually so that the deleted element disappears.

Link to the entire repository just in case.

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