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

93
Vistas
ReactJS component state change rendered but then immediately revert back to initial state

I've just started learning frontend development using React following this tutorial. All is well until I've come to the props & state learning section.

In this section, I was supposed to build a meme generator page. Everything went well, I followed as instructed by the tutorial. However, when I clicked the button to change the meme image, it didn't worked. The image stayed the same as the initial image (with initial image URL value/state), even though my code didn't differ so much from what I've seen in the tutorial.

Here's my code for my meme generator component:

import React from 'react'
import './style.css'
import memesData from "./constants/MemeDB"

function Meme() {

    const [meme, setMeme] = React.useState({
        topText: "",
        bottomText: "",
        randomImage: "https://i.imgflip.com/30b1gx.jpg",
        imageAlt: "Drake Hotline Bling"
    })

    const [allMemeImage, setAllMemeImage] = React.useState(memesData)
    
    function getNewMeme() {
        const memesArray = allMemeImage.data.memes
        const randomNumber = Math.floor(Math.random() * memesArray.length)
        const url = memesArray[randomNumber].url
        const name = memesArray[randomNumber].name
        setMeme(prevMeme => ({
            ...prevMeme,
            randomImage: url,
            imageAlt: name
        }))
    }

    return (
        <main>
            <form className='form'>
                <input
                    type="text"
                    className='form--input'
                    placeholder='Top text'
                />
                <input
                    type="text"
                    className='form--input'
                    placeholder='Bottom text'
                />
                <button onClick={getNewMeme} className='form--button'>Get a new meme image 🖼</button>
            </form>
            <img src={meme.randomImage} alt={meme.imageAlt} className='meme-image' />
        </main>
    )
}


export default Meme

For additional information, I build the page locally using Microsoft VSCode, instead of Scrimba, through the npx create-react-app command.

I tried to add console.log(randomNumber) and console.log(url) inside the getNewMeme function to make sure if the values changed if I clicked the button. Then, I've found out that those values actually changed. However, each time I clicked the button, the values of the randomNumber and the url are only displayed on the console for a split second before the console automatically was cleared again. Also sometimes, on the page, when I clicked the button, I could see a glimpse of the meme image changed before reverted back to the initial image/state. What is the problem here?

I'm sorry if I couldn't describe the problem correctly.

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

0

The problem is that, you are refreshing the page after each button click. To fix this:

function getNewMeme(event) { // <--- Accept the `event` parameter here
  event.preventDefault(); // <--- Prevent the default behavior of form submission, which is refreshing the page
  const memesArray = allMemeImage.data.memes;
  const randomNumber = Math.floor(Math.random() * memesArray.length);
  const url = memesArray[randomNumber].url;
  const name = memesArray[randomNumber].name;
  setMeme((prevMeme) => ({
    ...prevMeme,
    randomImage: url,
    imageAlt: name,
  }));
}

How? This is not related to React, but rather HTML. By default, a <button> has the type submit, and if placed inside a form, it will automatically submit the form when clicked. Again by default, form submission will cause the page to reload. We want to prevent this behavior.

Reference:

  • Button's type: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type
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