necesito volver a renderizar un componente en mi aplicación esto es lo que he hecho
let path = window.location.href; useEffect(() => { alert("changed"); }, [window.location.href]); estoy usando window.history.pushState(null, null, window.location.pathname + "some url" ); en uno de los componentes secundarios
que necesito detectar en este componente principal anterior, pero no recibo una alert("changed") cada vez que hay un cambio en la URL
Crea un gancho, algo como:
const useReactPath = () => { const [path, setPath] = React.useState(window.location.pathname); const listenToPopstate = () => { const winPath = window.location.pathname; setPath(winPath); }; React.useEffect(() => { window.addEventListener("popstate", listenToPopstate); return () => { window.removeEventListener("popstate", listenToPopstate); }; }, []); return path; };Luego, en su componente, úselo así:
const path = useReactPath(); React.useEffect(() => { // do something when path changes ... }, [path]);Por supuesto, tendrás que hacer esto en un componente superior.