0

In React.js if I want to trigger a function when a certain value changes I would use the useEffect hook and pass it a dependency array containing the value that should cause that function to trigger:

const [myState,setMyState] = useState("some-value");

useEffect(
   () => {
      someFunctionToTrigger();
   }, [theValue]
);

However I'd like to acheive that in pure JavaScript, more specifically I want to know when my location.href changes so I could call a function every time it changes:

let location_href = location.href;

// everytime location.href changes I want to call a function so I need the equivalent of useEffect

Note that using a Proxy doesn't work with location

Daniel_Kamel
  • 610
  • 8
  • 29
  • Does this answer your question? [Listening for variable changes in JavaScript](https://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript) – pistou Jun 16 '23 at 07:40
  • 3
    This may answer your question - [Event when window.location.href changes](https://stackoverflow.com/questions/3522090/event-when-window-location-href-changes) – Shaurya Singh Jun 16 '23 at 08:07

0 Answers0