title: React.js Music-Player: How can I play audio when list element is clicked?
body: <p>Good afternoon all,</p>
  <p>I am a new programmer and I recently started following a music-player tutorial, and am experiencing a couple of issues with it.</p>
  <p><strong>Project Overview:</strong></p>
  <p>As previously described, it is a music-player project made with <code>create-react-app</code>.  The objective is to click the image of the song of your choosing, and for the song to be played for you.</p>
  <p>The songs are saved as mp3’s  in a folder named <code>music</code>. The <code>music</code> folder is located in the src directory. Each song is assigned to its given song image (as you’ll see on the code I provided below). Then the songs/images are wrapped in an <code>&lt;ol&gt;</code> list element.</p>
  <p><strong>The Problem:</strong></p>
  <p>I am having trouble with playing the audio when the image is clicked. I’ve integrated a lot of the advice I’ve recieved from different users. Though i’m grateful for the result, which is that the code compiles (no error messages), I’m still experiencing the same issue, and that is that the audio doesn’t play when clicked.</p>
  <p>So to clarify, <strong>How can I make it so that when I click the list element, It will play the assigned audio?</strong>  Please correct my code and provide an example of the solution using the code I’ve provided below. Due to my novice skills, I am not familiar with any complex terms.</p>
  <h3>The Code:</h3>
  <p><strong>Turkish.js</strong> <em>(the music-player file)</em>:</p>
  <pre><code>import React, { Component,audio,useRef } from 'react';
  
  import turk1 from &quot;../music/turk1.mp3&quot;;
  import turk2 from &quot;../music/turk2.mp3&quot;
  import turk3 from &quot;../music/turk3.mp3&quot;
  import turk4 from &quot;../music/turk4.mp3&quot;
  
  const data = [
      { imgSrc: 'turk1.png', audioSrc: turk1 },
      { imgSrc: 'turk2.png', audioSrc: turk2 },
      { imgSrc: 'turk3.png', audioSrc: turk3 },
      { imgSrc: 'turk4.png', audioSrc: turk4 },
    ];
  
    export default class Turkish extends Component {
      render() {
        return (
          &lt;div&gt;
            &lt;ol&gt;
              {data.map(({ imgSrc, audioSrc }) =&gt; (
                &lt;MediaComponent imgSrc={imgSrc} audioSrc={audioSrc} /&gt;
              ))}
            &lt;/ol&gt;
          &lt;/div&gt;
        );
      }
    }
    
    const MediaComponent = ({ imgSrc, audioSrc }) =&gt; {
      const audioRef = useRef(null);
      const toggleAudio = () =&gt;
        audio.ref.current.paused
          ? audioRef.current.play()
          : audioRef.current.pause();
      return (
        &lt;li&gt;
          &lt;img src={imgSrc} onClick={toggleAudio}/&gt;
          &lt;audio ref={audioRef} src={audioSrc} /&gt;
        &lt;/li&gt;
      );
    };
  
  </code></pre>
  <p><strong>Package.json:</strong></p>
  <pre><code>
  {
    &quot;name&quot;: &quot;spotifly&quot;,
    &quot;version&quot;: &quot;0.1.0&quot;,
    &quot;private&quot;: true,
    &quot;dependencies&quot;: {
      &quot;@testing-library/jest-dom&quot;: &quot;^5.16.4&quot;,
      &quot;@testing-library/react&quot;: &quot;^12.1.5&quot;,
      &quot;@testing-library/user-event&quot;: &quot;^13.5.0&quot;,
      &quot;bootstrap&quot;: &quot;^5.1.3&quot;,
      &quot;navbar&quot;: &quot;^2.1.0&quot;,
      &quot;react&quot;: &quot;^18.0.0&quot;,
      &quot;react-audio-player&quot;: &quot;^0.17.0&quot;,
      &quot;react-bootstrap&quot;: &quot;^2.2.3&quot;,
      &quot;react-bootstrap-icons&quot;: &quot;^1.8.1&quot;,
      &quot;react-dom&quot;: &quot;^18.0.0&quot;,
      &quot;react-is&quot;: &quot;^18.0.0&quot;,
      &quot;react-router-dom&quot;: &quot;^6.3.0&quot;,
      &quot;react-scripts&quot;: &quot;5.0.0&quot;,
      &quot;web-vitals&quot;: &quot;^2.1.4&quot;
    },
    &quot;scripts&quot;: {
      &quot;start&quot;: &quot;react-scripts start&quot;,
      &quot;build&quot;: &quot;react-scripts build&quot;,
      &quot;test&quot;: &quot;react-scripts test&quot;,
      &quot;eject&quot;: &quot;react-scripts eject&quot;
    },
    &quot;eslintConfig&quot;: {
      &quot;extends&quot;: [
        &quot;react-app&quot;,
        &quot;react-app/jest&quot;
      ]
    },
    &quot;browserslist&quot;: {
      &quot;production&quot;: [
        &quot;&gt;0.2%&quot;,
        &quot;not dead&quot;,
        &quot;not op_mini all&quot;
      ],
      &quot;development&quot;: [
        &quot;last 1 chrome version&quot;,
        &quot;last 1 firefox version&quot;,
        &quot;last 1 safari version&quot;
      ]
    }
  }
  
  </code></pre>
  <p><strong>Picture of the Music folder:</strong></p>
  <p><a href="https://i.stack.imgur.com/71wOD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/71wOD.png" alt="enter image description here" /></a></p>
  <p><strong>When I click on the song file, it tells me this:</strong></p>
  <p><a href="https://i.stack.imgur.com/b9d2Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b9d2Q.png" alt="enter image description here" /></a></p>
  <p>I really appreciate your time &amp; effort, let me know what you think!</p>
  <p>Best,</p>
  <p>-Zpo</p>