mardi 21 avril 2020

useLocation().pathname in react-router-dom is changing, but when I put it in an if statement, it keeps saying true

I'm making a react app. I'm importing useLocation and putting it into a variable (location). I'm checking if pathname is NOT "/" it should output false, but it keeps saying true in every page.

import React, { useState, useEffect } from "react";
import { useLocation } from "react-router-dom";

function Header() {
  const [inIndex, setInIndex] = useState();
  let location = useLocation();

  useEffect(() => {
    //Checks if location.pathname is not "/".
    if (location.pathname !== "/") setInIndex(false);
    else setInIndex(true);
  }, []);

  console.log(inIndex); //Keeps saying true

  return null;
}

export default Header;

Thank you in advance!

Edit: I also checked what location.pathname is and it's "/" and it's a string.

Aucun commentaire:

Enregistrer un commentaire