Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
cómo volver a representar una página usando el gancho UseEffect en window.innerWidth en reaccionar

Quiero hacer algunos cambios dependiendo del tamaño de la pantalla

 useEffect(() => { console.log("am called"); if (window.innerWidth < 800) { document.getElementsByTagName("body")[0].style.display = "none"; } if (window.innerWidth > 800) { document.getElementsByTagName("body")[0].style.display = "block"; } }, [window.innerWidth]);

cual es el problema con este codigo

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Debe observar el cambio de tamaño de la ventana y poner este valor en estado.

 const [width, setWidth] = useState(0) useEffect(() => { const onResize = () => setWidth(window.innerWidth) window.addEventListener('resize', onResize); return () => { window.removeEventListener("resize", onResize) } }, [setWidth]) //and then useEffect(() => { console.log("am called"); if (width< 800) { document.getElementsByTagName("body")[0].style.display = "none"; } else { document.getElementsByTagName("body")[0].style.display = "block"; } }, [width]);

mejor solucion

 export default function useMatchMedia(condition) { const [isMatching, setIsMatching] = useState(false); useEffect( function updateMatchMedia() { if (!condition) return setIsMatching(true); const updateIsMatching = (event) => setIsMatching(event.matches); const matcher = window.matchMedia(condition); setIsMatching(matcher.matches); matcher.addEventListener('change', updateIsMatching); return () => { matcher.removeEventListener('change', updateIsMatching); }; }, [condition] ); return [isMatching]; }

entonces puedes usarlo

 isTablet = useMatchMedia(`(max-width: 800px)`); useEffect(() => { if(isTablet){ } else { } }, [isTablet])
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda