Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Cómo colocar un ícono en una declaración if then para un botón de reacción

Estoy tratando de crear un botón que muestre un ícono y un mensaje diferentes cuando se haga clic en él. Sin embargo, no estoy seguro de cómo incluir el ícono dentro de la declaración 'si entonces' dentro del elemento < button > . Soy bastante nuevo en React.

Mi código está a continuación:

 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;

Recibo un error aquí {isPlay ? <Pause /> "pause" : <Play/> "listen to this page"}

el error es:

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

Intenté insertar el ícono dentro de las marcas "", pero eso solo muestra el código del botón como una cadena.

Gracias por tu ayuda

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puedes hacerlo así

 <button onClick={handlePlaying}> {isPlay ? ( <> <Pause /> Pause </> ) : ( <> <Play /> Listen to this page </> )} </button>;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!