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

150
Visualizações
How to place an icon on an if then statement for a react button

I'm trying to create a button that will display a different icon and message when it is clicked. However, I'm unsure of how to include the icon inside the 'if then' statement within the < button > element. I'm quite new to React.

My code is below:

import React, { setState, useState, useEffect } from "react";
import { Pause, 
          Play  } from 'react-feather';

function AudioPlayer() {
  // use Audio constructor to create HTMLAudioElement
  const audioTune = new Audio("<YOUR_AUDIO_FILE_PATH.mp3>");

  // variable to play audio in loop
  const [playInLoop, setPlayInLoop] = useState(false);

  // variable to play audio in loop
  const [isPlay, isPlaying] = useState(0);

  // load audio file on component load
  useEffect(() => {
    audioTune.load();
  }, []);

  // play audio sound
  const playSound = () => {
    audioTune.play();
    isPlaying(1);
  };

  // pause audio sound
  const pauseSound = () => {
    audioTune.pause();
  };

  // stop audio sound
  const stopSound = () => {
    audioTune.pause();
    audioTune.currentTime = 0;
    isPlaying(0);
  };

  const handlePlaying = () => {
    if (isPlay) {
      stopSound();
    } else {
      playSound();
    }
  };

  return (
    <div>
      <div className="App">
    
        <button onClick={handlePlaying}>
          {isPlay ? <Pause /> "pause" : <Play/> "listen to this page"}
        </button>
      </div>
    </div>
  );
}

export default AudioPlayer;

I get an error here {isPlay ? <Pause /> "pause" : <Play/> "listen to this page"}

The error is:

SyntaxError
/src/components/Audio2.js: Unexpected token, expected ":" (57:30)

I've tried to insert the icon within the "" marks, but that is just displaying the button code as string.

Thanks for your help

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

0

You can do it like this

<button onClick={handlePlaying}>
  {isPlay ? (
    <>
      <Pause /> Pause
    </>
  ) : (
    <>
      <Play /> Listen to this page
    </>
  )}
</button>;
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