PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 10, 2022

[FIXED] How can I send an audio reference to a function in React?

 July 10, 2022     audio, function, reactjs, reference     No comments   

Issue

I have an audio reference:

    const sound1 = useRef();

as I made it by:

   <audio  ref={sound1} src='/resources/DRUMS.mp3' loop={looping}></audio>

and a function I wnat to send it to:

const toggleMuteUnmute=(soundRef, isPlaying, stateFunc)=>{
    const prevValue = isPlaying;
    stateFunc(!prevValue);
    if(prevValue){
        soundRef.current.muted = false;
        animationRef.current = requestAnimationFrame(whilePlaying);
    }
    else{
        soundRef.current.muted = true;
        cancelAnimationFrame(animationRef.current);
    }
}

using this:

<button onClick={toggleMuteUnmute(sound1, isPlaying1, setIsPlaying1)} >

but an error comes up:

Cannot set properties of undefined (setting 'muted')

reffering to the first line in the 'else' statement.

Why can't it read the muted attribute of the html audio tag?


Solution

Ok I solved it. All I had to do was to put - () => before the function call, as in -

<button onClick={() => toggleMuteUnmute(sound1, isPlaying1, setIsPlaying1)} >


Answered By - Nadav Holtzman
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing