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

121
Vistas
React code keeps rerendering after changing state

this code keeps rerendering the page on every state change and when I add an image it keeps rerendering infinitely without errors

const [adData, setAdData] = useState({
    name: "",
    link: "",
    fromDate: {},
    toDate: {},
    fromTime: {},
    toTime: {},
    iconEn: null,
    allViews: 0,
    userViews: 0,
  });
  const [iconEN, setIconEN] = useState(null);
  const [currentEnImg, setCurrentEnImg] = useState(null);
  const handleOnChange = (e) => {
    setAdData({ ...adData, [e.target.name]: e.target.value });
  };
  const handleOnCheck = (e) => {
    setAdData({ ...adData, [e.target.name]: e.target.checked});
  };
  if (iconEN) {
    const reader = new FileReader();
    reader.onload = () => {
      setCurrentEnImg(reader.result);
      setAdData({ ...adData, iconEn: iconEN });
    };
    reader.readAsDataURL(iconEN);
  }

what am I doing wrong and how to fix it.

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

0

you can just add useEffects hook when page load

useEffect(()=>{
  /*
  Query logic
  */
  console.log('i fire once');},[]);

in this hook call your that function which call when entering the page after use, this code does not render every time when change state it might help

about 4 years ago · Juan Pablo Isaza Denunciar

0

Issue is here:

if (iconEN) {
    const reader = new FileReader();
    reader.onload = () => {
      setCurrentEnImg(reader.result);
      setAdData({ ...adData, iconEn: iconEN });
    };
    reader.readAsDataURL(iconEN);
  }

every state set function re-render the application and on every re-render the same state set function is called that makes this process as a never ending loop.

Solution: try useEffect hook with a proper dependency variable or [] and this problem will be solved.

In your case put your login inside useEffect like:

useEffect(() => {
     if (iconEN) {
        const reader = new FileReader();
        reader.onload = () => {
          setCurrentEnImg(reader.result);
          setAdData({ ...adData, iconEn: iconEN });
        };
        reader.readAsDataURL(iconEN);
      }
}, []);
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