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

142
Visualizações
Loader until images loaded react

I have a task - make preloader until all images get loaded. So we using react, and I'm new in this framework, I need help because of I can't make right logical steps. We have App.js and this file doesn't have any img in return only components, so how to get info from images that they have loaded? I know about onLoad, but don't understand how to get this bool from onLoad which is in components.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are several ways to do this, but the simplest is to display the final image hidden, and then flip it to visible once it loads.

   class Foo extends React.Component {
  constructor(){
    super();
    this.state = {loaded: false};
  }

  render(){
    return (
      <div>
        {this.state.loaded ? null :
          <div
            style={{
              background: 'red',
              height: '400px',
              width: '400px',
            }}
          />
        }
        <img
          style={this.state.loaded ? {} : {display: 'none'}}
          src={this.props.src}
          onLoad={() => this.setState({loaded: true})}
        />
      </div>
    );
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I've never had to deal with this but there is a way (which might be a bit hacky).

You can essentially use a useEffect hook that is built into React. This is used in the functional components. It essentially runs commands after the page has mounted. What you can try, is initially set a boolean state (e.g.: isLoading) to be true and then run a command to set this state to false in the useEffect hook.

Example 👇

function MyComponent(){
  // Create an instance of react's useState hook
  const [isLoading, setIsLoading] = useState(true);

  // This will run after the page has mounted
  React.useEffect(() => {
    setIsLoading(false);
  }, [setIsLoading]);

  return{
    //... 
  }
}
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