Necesito ejecutar sonidos automáticamente en React. Para ejecutar sonidos, utilizo un gancho useSound no estándar. Crea un botón y al hacer clic en este botón comienza el sonido. Hice un disparador para iniciar automáticamente un clic e hice un retraso entre los sonidos.
Los sonidos vienen a la vista de la matriz. Por lo tanto, reviso la matriz y creo varios botones en los que se debe hacer clic. Dime cómo crear para cada botón const [play] = useSound("/static/1.mp3"); y su identificación única para que todos los botones se creen y reproduzcan automáticamente?
import React, { useState, useEffect } from "react"; import useSound from "use-sound"; export default function Play(props) { const arr = ['./1.mp3', '/static/2.mp3', '/static/3.mp3'] function Button() { return props.newResult.map(() => ( <button className={styles.buttonPlay} id='button3'> {props.newResult} </button> )); } const Play = () => { const [play] = useSound("/static/1.mp3"); // Буква return ( <> <button className={styles.buttonPlay} id='button' onClick={play}> Пациент </button> </> ); }; async function Auto() { await new Promise(function (s) { setTimeout(s, 1000); }); document.querySelector("#button").click(); await new Promise(function (s) { setTimeout(s, 1000); }); } useEffect(() => { Auto(); }, []); return ( <> {Play()} </> ); }