Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

90
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda