skip to Main Content

I have a component which by default is a button. But when pressed, it is replaced by four other buttons, each of which has a PNG image.

import React, {useState} from 'react';
import styles from './CallButton.module.css'
import whatsAppImg from '../../images/whatsapp.png'
import telegramImg from '../../images/telegram.png'

const CallButton = () => {
    const [isActive, setIsActive] = useState(false);

    return (<div className={styles.container}>
        {isActive ?
            <div className={styles.appearsLeft}>
                <a className={styles.callButton} title={'Написать в WhatsApp'} href="https://wa.me/+79147726787">
                    <img src={whatsAppImg} className={styles.icon}/>
                </a>
                <a className={styles.callButton} title={'Написать в Telegram'} href="https://t.me/yolkin27">
                    <img src={telegramImg} className={styles.icon}/>
                </a>
            </div> :
            <div className={styles.main}
                 onClick={() => setIsActive(true)}>
                <div className={styles.notActiveButton}>Связаться со мной</div>
            </div>
        }
    </div>);
};

export default CallButton;

In idle state it looks like this:

enter image description here

And in active state:

enter image description here

But the fact is that the pictures in the buttons start loading only after the transition to the active state. And I would like them to be loaded in advance. Is there a way to do this?

2

Answers


  1. You can farce the images loading by calling Image constructor at the moment of component mounting.

    Your code should look like this:

    import React, {useState, useEffect} from 'react';
    import styles from './CallButton.module.css'
    import whatsAppImg from '../../images/whatsapp.png'
    import telegramImg from '../../images/telegram.png'
    
    const imagesSrcs = [whatsAppImg, telegramImg];
    
    const CallButton = () => {
      const [isActive, setIsActive] = useState(false);
    
      useEffect(() => {
        imagesSrcs.forEach(imageSrc => {
          (new Image()).src = image;
        })
      }, []);
    
      return (<div className={styles.container}>
        {isActive ?
          <div className={styles.appearsLeft}>
            <a className={styles.callButton} title={'Написать в WhatsApp'} href="https://wa.me/+79147726787">
              <img src={whatsAppImg} className={styles.icon}/>
            </a>
            <a className={styles.callButton} title={'Написать в Telegram'} href="https://t.me/yolkin27">
              <img src={telegramImg} className={styles.icon}/>
            </a>
          </div> :
          <div className={styles.main}
               onClick={() => setIsActive(true)}>
            <div className={styles.notActiveButton}>Связаться со мной</div>
          </div>
        }
      </div>);
    };
    
    export default CallButton;
    
    Login or Signup to reply.
  2. Assigning the images code to a variable, and rendering the variable conditionally, should get you the result you’re after.

    i.e:

    let socialImages = (<div className={styles.appearsLeft}>...);
    

    Then:

    {isActive ? socialImages : <div>...</div>}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search